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 10 of 10
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    27
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Trouble with onsubmit event in form

    I'm trying to test some input boxes on submit, but its not working for some reason I tried playing with the action and method in the form with no luck. Maybe its my script but i couldn't find where. It should run the script on click of the submit. Thanks


    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <title>Chapter 11</title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <script type="text/javascript">
    function submit(){
    if(document.forms[0].visitor_name.value == "" || document.forms[0].visitor_name.value == "Enter your name"){
    window.alert("You did not enter your name");
    return false;
    }else if(document.forms[0].email.value == ""){
    window.alert("You did not enter your email address");
    return false;
    }else if(document.forms[0].pass1.value != document.forms[0].pass2.value || 
    document.forms[0].pass1.value == "" || document.forms[0].pass2.value == ""){
    window.alert("Your passwords do not match");
    }
    return true;
    }
    </script>
    </head>
    <body>
    <h1>Web Site Registration Form</h1>
    <form action="" method="post" onsubmit="return submit()">
    <h2>Personal Information</h2>
    <p>Name
    <br>
    <br>
    <input type="text" value="Enter your name" name="visitor_name" 
    onclick="if (this.value == 'Enter your name') this.value = '';"
    size="50">
    <br>
    <br>
    E-mail address
    <br>
    <br>
    <input type="text" value="Enter your e-mail address" name="email" 
    onclick="if (this.value == 'Enter your e-mail address') this.value = '';"
    size="50">
    <br>
    </p>
    <h2>Security Information</h2>
    <p>Enter a password that you can use to manage your subscription online
    <br>
    <br>
    <input type="text" name="pass1" value="" size="50">
    <!-- onblur??? -->
    <br>
    <br>
    Type the password again to confirm it
    <br>
    <br>
    <input type="text" name="pass2" value="" size="50">
    <br>
    <br>
    Security question
    <br>
    <br>
    <select name="question">
    <option value="mother">What is your mother's maiden name?</option>
    <option value="pet">What is the name of pet?</option>
    <option value="color">What is your favorite color?</option>
    </select>
    <br>
    <br>
    Your answer
    <br>
    <br>
    <input type="text" name="ans" size="50">
    <br>
    </p>
    <h2>Preferences</h2>
    <p>Send special offers to my e-mail address
    <input type="radio">
    <input type="radio">
    <br>
    <br>
    Select areas of interest(select at least one)
    <br>
    <br>
    <input type="checkbox">
    <br>
    <input type="checkbox">
    <br>
    <input type="checkbox">
    <br>
    <input type="checkbox">
    <br>
    <input type="checkbox">
    <br>
    <br>
    </p>
    <p><input type="submit" /></p>
    </form>
    </body>
    </html>

    Thanks again.
    Last edited by CodyJava; 11-15-2012 at 02:01 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    When posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.

    Form validation of the pattern if (document.formname.formfield.value == "") - that is blank - 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. A proper name may only contain letters, hyphen, space and apostrophe.
    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.

    function submit(){

    You should avoid giving names or id's to your variables/functions/arguments/forms words which are HTML/JavaScript methods/properties/attributes such as 'name' or 'id' or 'value' or 'test' or 'text' or 'checked' or 'click' or 'href' or 'closed' or 'go' or 'submit' or 'replace' or 'button' or 'radio' or 'parseInt'.

    Instead of repeating ad nauseam document.forms[0] just create a new variable
    var f = document.forms[0];


    Why does it not work? Hint - look for missing closing braces.


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 11-15-2012 at 02:02 PM.

    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.

  • Users who have thanked Philip M for this post:

    CodyJava (11-15-2012)

  • #3
    New Coder
    Join Date
    Sep 2012
    Posts
    27
    Thanks
    23
    Thanked 0 Times in 0 Posts
    its a homework assignment and we were suppose to call it submit() so no answer on why it doesn't work just critique?

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by CodyJava View Post
    its a homework assignment and we were suppose to call it submit() so no answer on why it doesn't work just critique?
    I said - Why does it not work? Hint - look for missing closing braces.

    We are not here to do your homework for you.

    If your teacher has told you to name a function submit() - get a new teacher!

    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.

  • Users who have thanked Philip M for this post:

    CodyJava (11-15-2012)

  • #5
    New Coder
    Join Date
    Sep 2012
    Posts
    27
    Thanks
    23
    Thanked 0 Times in 0 Posts
    I checked all my brackets and it validates, also I posted that before you edited your post.
    Also I typed all that code you did not all I need is help with a problem, what is this forum for if you can't get help.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Did you rename the function submit() to something else?

    I now see that you have indeed placed closing braces on the same line as the next statement.
    }else if(document.forms[0].email.value == ""){
    Not recommended for clarity!

    Do not use else if in this context. Each if statement is independent and not contingent on a previous one.

    Code:
    <script type="text/javascript">
    
    function sub(){
    if(document.forms[0].visitor_name.value == "" || document.forms[0].visitor_name.value == "Enter your name"){
    window.alert("You did not enter your name");
    return false;a 
    }
    if ((document.forms[0].email.value == "") || (document.forms[0].email.value == "Enter your e-mail address")) {
    window.alert("You did not enter your email address");
    return false;
    }
    if(document.forms[0].pass1.value != document.forms[0].pass2.value || 
    document.forms[0].pass1.value == "" || document.forms[0].pass2.value == ""){
    window.alert("Your passwords do not match");
    return false;
    }
    return true;
    }
    </script>
    But as I have pointed out this sort of form validation (check if a field is blank) is virtually useless. A single space will return false.
    Last edited by Philip M; 11-15-2012 at 06:53 PM.

    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.

  • Users who have thanked Philip M for this post:

    CodyJava (11-15-2012)

  • #7
    Banned
    Join Date
    Oct 2012
    Posts
    81
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by Philip M View Post

    [CODE]<script type="text/javascript">
    ...
    return false;a
    ....
    Your signature says

    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.
    so what is the red a in your code for ?

  • #8
    New Coder
    Join Date
    Sep 2012
    Posts
    27
    Thanks
    23
    Thanked 0 Times in 0 Posts
    Thanks I changed the name of the function and used separate if statements and it worked. The assignment was to display an error message if nothing is entered in the input boxes that's why I left them blank, otherwise I wouldn't leave them blank. Thanks for your help.

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by minder View Post
    Your signature says



    so what is the red a in your code for ?
    No idea how that got there, bullant. It is not in the code I tested. I must have pressed the a key instead of something else when I added my remark about form validation. But thank you for pointing it out - it might have caused a problem for CodyJava.
    Last edited by Philip M; 11-15-2012 at 07:48 PM.

    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
    Banned
    Join Date
    Oct 2012
    Posts
    81
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by Philip M View Post
    It is not in the code I tested.
    yeah yeah, whatever


  •  

    Posting Permissions

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