Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Problem with form validator

    Hello,

    I don't see what the problem is with my form validation script. It does not validate. Please point it out to me.

    Code:
    <script type="text/javascript">
    function validateForm()
    {
    var x=document.forms["myForm"]["name"].value;
    if (x==null || x=="")
      {
      alert("First name must be filled out");
      return false;
      }
    
    var x=document.forms["myForm"]["email"].value;
    var atpos=x.indexOf("@");
    var dotpos=x.lastIndexOf(".");
    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
      {
      alert("Please enter a valid email address");
      return false;
      }
    
    var x=document.forms["myForm"]["message"].value;
    if (x==null || x=="")
      {
      alert("You have not entered a message");
      return false;
      }
    }
    </script>
    html

    Code:
    <form name="myForm" action="contact.php" onsubmit="return validateForm()" method="post">
    First name: 
    </p>
    <input type="text" name="name">
    </p>
    Email address:
    </p>
    <input type="text" name="email">
    </p>
    Subject: 
    </p>
    <input type="text" name="subject">
    </p>
    Message:
    </p>
    <textarea name="message" rows="15" cols="50"></textarea>
    </p>
    <input type="submit" value="Submit">
    </form>
    the script is in the <head> section and html in the <body>.

    Thank you.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    It works for me. What exactly do you think is wrong? What error messages do you receive? Have you tried putting in alerts at strategic positions to observe the progress of the validation?

    Form validation of the pattern if (document.forms[0].elements[1].value == "") is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. Numeric values, such as zip codes and phone numbers, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.

    A form field can never be null - only "" (a blank field)

    "If you make it idiot proof, they'll build a better idiot"

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    Jan 2012
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for the tips, tested it in firefox... i guess it doesn't work in opera only.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    I don't see why it should not work in any browser, but try changing

    name="name" to something else like name="username"

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by gingerbread View Post
    Thanks for the tips, tested it in firefox... i guess it doesn't work in opera only.
    When I copy and paste your code into a html file it works fine in my opera v10, although the name attribute in <form> is deprecated - but that's another issue.

    Post your html file that you say does not work in opera and the opera version number you are using.

  • #6
    New Coder
    Join Date
    Jan 2012
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hello there,

    I'm using Opera 11.60

    Turbo is off...

    If the name attribute is depreciated, would it be better to do away with it or it does not matter either way?

    Thanks guys for your help!!!

    Code:
    <?php defined( '_JEXEC' ) or die( 'Restricted access' );?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" 
       xml:lang="<?php echo $this->language; ?>" lang="<?php echo $this->language; ?>" >
    
    <head>
    <link rel="stylesheet" href="/template.css" type="text/css" />
    
    <script type="text/javascript">
    function validateForm()
    {
    var x=document.forms["myForm"]["name"].value;
    if (x==null || x=="")
      {
      alert("First name must be filled out");
      return false;
      }
    
    var x=document.forms["myForm"]["email"].value;
    var atpos=x.indexOf("@");
    var dotpos=x.lastIndexOf(".");
    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
      {
      alert("Please enter a valid email address");
      return false;
      }
    
    var x=document.forms["myForm"]["message"].value;
    if (x==null || x=="")
      {
      alert("You have not entered a message");
      return false;
      }
    }
    </script>
    
    </head>
    
    <body>
    
    
      <div id="wrapper">
    
      <div id="content"> 
    <div id="logo"><img src="http://url.com/images/logo_edited.jpg"></div>
    
    <!-- Main content -->
    
    <div id="contact">
    <h2>Contact Us</h2>
    </p>
    <form name="myForm" action="contact.php" onsubmit="return validateForm()" method="post">
    First name: 
    </p>
    <input type="text" name="name">
    </p>
    Email address:
    </p>
    <input type="text" name="email">
    </p>
    Subject: 
    </p>
    <input type="text" name="subject">
    </p>
    Message:
    </p>
    <textarea name="message" rows="15" cols="50"></textarea>
    </p>
    <input type="submit" value="Submit">
    </form>
    </div>
    
    <br>
    
    <div class="footer">
    <ul class="foot">
    </ul>
    </div>
    <br>
    <br>
    </div>
    </div>
    </body>
    </html>

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    The name attribite of <form> is indeed deprecated but I would have thought it will be continue to be supported for many years.

    However, you could try changing throughout
    forms["myForm"]
    to forms[0]

    You can test by removing chunks of the validation to see which one (if any) is the culpit.
    Last edited by Philip M; 03-02-2012 at 08:00 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #8
    New Coder
    Join Date
    Jan 2012
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hey Phillip,

    It's not the form... it works in firefox and other browsers. For some reason javascript is not working on opera - even my javascript sharethis widget isn't displaying... Javascript is enabled on my browser. This is really weird.
    Last edited by gingerbread; 03-02-2012 at 11:36 AM.

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Well, it must be something in your browser settings. Are you quite sure that Javascript is enabled? That would be an obvious explanation.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #10
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by gingerbread View Post
    Hello there,

    I'm using Opera 11.60

    Turbo is off...
    To check/change the Javascript enabled state, all you have to do is press F12.
    If the name attribute is depreciated, would it be better to do away with it or it does not matter either way?
    An issue regarding the use of 'name' as a field name is that name it is a native property of the form object. In this case it won't cause a problem but it's a practice to avoid.

  • #11
    New Coder
    Join Date
    Jan 2012
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks.

    Javascript is enabled. Even my sharethis widgets aren't showing and I've upgraded to the latest Opera yet the issue persists. I'm using Joomla, could that be the problem? But I don't see how since my scripts work with other browsers.

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by gingerbread View Post
    Thanks.

    Javascript is enabled. Even my sharethis widgets aren't showing and I've upgraded to the latest Opera yet the issue persists. I'm using Joomla, could that be the problem? But I don't see how since my scripts work with other browsers.
    Make the changes which I and others have suggested (including improving your email validation) and see what happens then. Change the name of the variable x in case there is a clash. But really it must be something to do with your browser settings. Either that or Opera is not fit for purpose, which seems unlikely.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #13
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by gingerbread View Post
    If the name attribute is depreciated, would it be better to do away with it or it does not matter either way?
    Although it's deprecated browsers continue to support it just like most other deprecated syntaxes, but there is no guarantee they wil continue to support them forever.

    I would get rid of it and give the <form> an id instead. That way the <form> will still work ok if/when deprecated elements cease to be supported. but the name attribute is not the problem in this case. I was just making you aware that name should not be used.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •