Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2011
    Thanked 0 Times in 0 Posts

    [code]Please help with looping through form validation[code]

    I am trying to set up a looping structure that tests to see if the user enters a value. If the textbox is null then a global variable is false otherwise a checkbox is checked and the global variable is true.

    below is what i have done so far, please assist me.

    var isValid = false;

    window.onload = startForm;

    function startForm() {

    document.forms[0].onsubmit = checkEntries;

    alert("You have been added to the list")

    function checkEntries() {
    var menus = new Array();
    var formObject = document.getElementsByTagName('*');
    for (var i=0; i < formObject.length; i++){
    if (formObject[i] == "myform") menus.push(formObject[i]);
    if (document.forms[0].firstName.value.length==0 || document.forms[0].firstName.value.length == null){
    isValid= false;
    alert("Please enter a first name");
    } else (document.forms[0].check0.checked=true);

    if (document.forms[0].lastName=="" || document.forms[0].lastName== null){
    alert("Please enter a last name");
    isValid = false;
    } else (document.forms[0].check1.checked=true);

    if (document.forms[0].email=="" || document.forms[0].email== null) {
    alert("Please enter a valid email");
    } else
    return (document.forms[0].check0.checked=true);

    if (document.forms[0].bDate=="" || document.forms[0].bDate== null) {
    alert("please make sure you enter a valid birth date.");
    else (document.forms[0].check0.checked=true);

    here is the form html...

    <form name="myform" >
    <input type="checkbox" name="check0" class="check0" id="check0" >
    First: <input type="text" name="firstName" id="firstName"> <BR>
    <input type="checkbox" name="check1" class="check1" id="check1" >
    Last: <input type="text" name="lastName" id="lastName" ><BR>
    <input type="checkbox" name="check2" class="check2" id="check2" >
    E-Mail: <input type="text" name="email" id="email"> <BR>
    <input type="checkbox" name="check3" class="check3" id="check3" >
    Birthday (mm/dd/yyyy): <input type="text" name="bDate" id="bDate"> <BR>
    <input type="submit" value="Join our mailing List" />

  2. #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    London, England
    Thanked 2,712 Times in 2,686 Posts
    Form validation of the pattern if (document.forms[0].firstName.value.length==0 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.

    I would advise that you recast your code to validate each entry separately.
    Hint - set your variable isValid to true at the outset, and reset it to false if any aspect of the validation fails.

    Call the validation script thus:-

    <form name="myform" onsubmit = "return checkEntries()">
    BTW, 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.

    You can't really win as England manager unless you win. Football Commentator ITV 1

  3. #3
    New to the CF scene
    Join Date
    Apr 2011
    Thanked 0 Times in 0 Posts

    thank you for your help

    I appreciate the help.


Posting Permissions

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