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
    Jul 2012
    Location
    Illinois
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool So close but cannot get it to validate

    Hi, does anyone know why I cannot get this to validate? It will clear the form alright.

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8" />
    <title>Validation Project</title>
    <script type="text/javascript">

    function num_checked(check_box_array)
    {

    var nchecked = 0;
    for (i = 0; i < check_box_array.length; i++)
    {
    if (check_box_array[i].checked)
    {
    nchecked++;
    }
    return nchecked;
    }
    //return nchecked;

    }

    function isAlphabetic(elem, helperMsg)
    {
    var alphaExp = /^[a-zA-Z]+$/;
    if (elem.value.match(alphaExp))
    {

    return true;
    } else {
    alert(helperMsg);
    elem.focus();
    return false;
    }
    }

    function notEmpty(elem, helperMsg)
    {

    if (elem.value.length==0)
    {
    alert(helperMsg);
    elem.focus();
    return false;
    }
    return true;
    }

    function isNumeric(elem, helperMsg)
    {
    var numericExpression = /^[0-9]+$/;
    if (elem.value.match(numericExpression))
    {
    return true;
    } else {
    alert(helperMsg)
    elem.focus();
    return false;
    }
    }

    function processFormData()
    {

    var name = document.getElementById('yourname');
    var yearborn = document.getElementById('birthyear');
    var location = document.getElementById('location');
    var gradyear = document.getElementById('graduation');
    var myfavorites = document.getElementById('favs');


    var name_warning_alpha = "Please enter letters only for your name!";
    var name_warning_missing = "Please enter your name!";
    var yearborn_warning = "Please enter the year you were born!";
    var location_warning = "Please select a location from the drop-down list!";
    var gradyear_warning = "Please select the year you will graduate!";
    var myfavorites_warning = "Please select at least one of your favorites from the options shown!";

    var message = "You entered the following registration information: \n\n";



    var allCorrect = true; // Set error flag to true initially

    if (isAlphabetic(name, name_warning_alpha))
    {
    allCorrect = false;
    if (notEmpty(name, name_warning_missing))
    {
    allCorrect = false;
    if (isNumeric(yearborn, yearborn_warning))
    {
    allCorrect = false;
    if (num_checked(location,location_warning) == 0))
    {
    allCorrect = false;
    if (num_checked(gradyear, gradyear_warning) !=1))
    {
    allCorrect = false;
    if (num_checked(myfavorites, myfavorites_warning) !=1))
    {
    allCorrect = false;

    } // end brace for my favorites check


    if (allCorrect)
    {

    message += "Name: " + name + "<br />" +
    "Born: " + yearborn + "<br />" +
    "Home City: " + location + "<br />" +
    "Graduated: " + gradyear + "<br />" +
    "Favorites: " + myfavorites + ".";

    return allCorrect;
    alert(message);
    }


    </script>
    </head>
    <body>
    <h1>Validating The Form</h1>

    <form name = "registration" method = "post" action = "" >


    <div class="question" id="yourname">
    <p>Please enter your full name:
    <input type = "text" name = "yourname">
    </div>
    <div class="question" id="birthyear">
    <p>Please enter the year in which you were born:
    <input type = "text" size = "5" name = "birthyear">
    </div>
    <div class="question" id="location">
    <p>Which city did you move from?
    <select name="location">

    <option value="0">Choose one</option>
    <option value="1">Omaha</option>
    <option value="2">Toledo</option>
    <option value="3">Indianapolis</option>
    <option value="4">Chicago</option>
    </select>
    </div>
    <div class="question" id="graduation">
    <p>When did you graduate from high school?<br>
    <input type = "radio" name = "graduation" value = "2010">1985<br>
    <input type = "radio" name = "graduation" value = "2011">1986<br>

    <input type = "radio" name = "graduation" value = "2012">1987<br>
    <input type = "radio" name = "graduation" value = "2013">1988<br>
    </div>
    <div class="question" id="favs">
    <p>What were your favorite things about the campus tour today? (Choose 1 to 3 options):
    <ol start="0">
    <li><input type="checkbox" name="favs" value="food">Tasty food
    <li><input type="checkbox" name="favs" value="facilities">Modern facilities
    <li><input type="checkbox" name="favs" value="programs">Interesting programs
    </ol>

    </div>
    <p>
    <input type = "reset">

    <input type = "submit" value = "Submit" onClick = "return processFormData();">
    </form>

    </body>
    </html>

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    You have a lot of syntax errors with your process form data function, mostly in your if statements. Extra parentheses and lacking of closing brackets.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    Regular Coder
    Join Date
    Sep 2002
    Posts
    452
    Thanks
    0
    Thanked 20 Times in 20 Posts
    document.getElementById('yourname');


    var form = document.registration;
    var name = form.yourname;
    NO Limits!! DHCreationStation.com
    ------------------------------------------------------------
    Broken items wanted for tinkerin'! PostItNow@BrokenEquipment.com
    Global Complaint Dept.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,592
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Try separating the JavaScript into a different file. The HTML validator is likely to report errors in JavaScript that don't really exist simply because it thinks the JavaScript is invalid HTML.
    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
    •