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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript Validation Error.

    My validation doesn't work. Please help. Here's the code:

    Code:
    <html>
    <body>
    <head>
    <font face="Candara">
    
    <title>Selection Validator</title>
    
    <script type>="text/javascript" language="Javascript">
    function validateChoice(genderForm)  {
    genderOption = -1
    for (i=0;   i<genderForm.genderChoice.length; i++)  {
    if (genderForm.genderChoice[i].checked)  {
    genderOption = i
    alert ("You are a " + genderForm.genderChoice[i].value)
    }
    }
    if (genderOption ==-1  {
    alert ("You must choose either Male or Female.")
    return false
    }
    return true
    }
    </script>
    </head>
    </font>
    </body>
    
    <nonscript>
    Javascript not detected! This page will not work properly.
    </nonscript>
    
    <form name ="myForm" onSubmit="return validateChoice(this)">
    
    <p>Select your gender: &nbsp;
    
    <input type="radio" name="genderChoice" value="Male" /> Male
    <input type="radio" name="genderChoice" value="Male" /> Female
    <input type="submit" value="Validate" />
    </p>
    
    </form>
    </html>

  • #2
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    224
    Thanks
    0
    Thanked 32 Times in 30 Posts
    What is the method of your form GET or POST ? Which file is going to treat the datas ?

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,092
    Thanks
    23
    Thanked 594 Times in 593 Posts
    Code:
    <html>
    <body>  DOES NOT BELONG HERE
    <head>
    <font face="Candara"> DOES NOT BELONG HERE AND IS DEPRECIATED
    
    <title>Selection Validator</title>
    
    <script type>="text/javascript" language="Javascript"> PLEASE CORRECT THIS IT IS TOTALLY WRONG
    function validateChoice(genderForm)  {
    genderOption = -1
    for (i=0;   i<genderForm.genderChoice.length; i++)  {
    if (genderForm.genderChoice[i].checked)  {
    genderOption = i
    alert ("You are a " + genderForm.genderChoice[i].value)
    }
    }
    if (genderOption ==-1  { MISSING A CLOSING )
    alert ("You must choose either Male or Female.")
    return false
    }
    return true
    }
    </script>
    </head>
    </font> DOES NOT BELONG
    </body>
    
    <nonscript>
    Javascript not detected! This page will not work properly.
    </nonscript> REMOVE THIS IT DOES NOT WORK CORRECTLY
    
    <form name ="myForm" onSubmit="return validateChoice(this)"> AS julien SAID, NEEDS AN METHOD  ALSO name ="myForm" S/B ID ="myForm" ALSO NEEDS AN ACTION
    
    <p>Select your gender: &nbsp;
    
    <input type="radio" name="genderChoice" value="Male" /> Male
    <input type="radio" name="genderChoice" value="Male" /> Female value="Male" S/B value="female"
    <input type="submit" value="Validate" />
    </p>
    
    </form>
    </html>
    Fix code and check again.
    Last edited by sunfighter; 01-10-2013 at 04:47 PM.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,620
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by sunfighter View Post
    Fix code and check again.
    You missed that the form is after the </body> tag instead of before it where it belongs.

    Also the page is missing a DOCTYPE and so will render differently in different browsers
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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