Form field validation errors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Theadmin77
    New Member
    • Nov 2006
    • 19

    Form field validation errors

    Ok ,i have to create a page to allow entry of user information;all fields must be validated and error displayed otherwise

    * First Name (limit 25 characters)
    * Last Name (limit 25 characters)
    * Street Address (limit 45 characters)
    * City (limit 25 characters)
    * State (2 character code )
    * Zip Code (5 digits only, must be numeric)
    * Date of Birth (mm/dd/yyyy format, day, month, and year must be valid)

    I am been trying to validate all fields, but i got a hard time doing it ...
    This is the code so far :

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>
    <SCRIPT LANGUAGE="JavaS cript">
    function isValidDate(dat eStr) {

    var datePat = /^(\d{1,2})(\/|)(\d{1,2})\2(\ d{4})$/;


    var matchArray = dateStr.match(d atePat); // is the format ok?

    if (matchArray == null) {
    alert("Date is not in a valid format.")
    return false;
    }
    month = matchArray[1]; // parse date into variables
    day = matchArray[3];
    year = matchArray[4];
    if (month < 1 || month > 12) { // check month range
    alert("Month must be between 1 and 12.");
    return false;
    }
    if (day < 1 || day > 31) {
    alert("Day must be between 1 and 31.");
    return false;
    }
    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
    alert("Month "+month+" doesn't have 31 days!")
    return false
    }
    if (month == 2) { // check for february 29th
    var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    if (day>29 || (day==29 && !isleap)) {
    alert("February " + year + " doesn't have " + day + " days!");
    return false;
    }
    }
    return true; // date is valid
    }
    // End -->
    </script>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitl ed Document</title>

    </head>

    <body>
    <form id="form1" name="form1" method="post" action="">
    <label>First Name
    <input name="First" type="text" id="First" maxlength="25" />
    </label>
    </form>
    <form id="form2" name="form2" method="post" action="">
    <label>Last Name
    <input name="Last" type="text" id="Last" maxlength="25" />
    </label>
    </form>
    <form id="form3" name="form3" method="post" action="">
    <label>Street Address
    <input name="Street" type="text" id="Street" value="" maxlength="45" />
    </label>
    </form>
    <form id="form4" name="form4" method="post" action="">
    <label>City
    <input name="City" type="text" id="City" maxlength="25" />
    </label>
    </form>
    <form id="form5" name="form5" method="post" action="">
    <label>State
    <select name="State" size="1" id="State">
    <option>FL</option>
    <option>GA</option>
    <option>NY</option>
    </select>
    </label>
    </form>
    <form id="form6" name="form6" method="post" action="">
    <label>Zip
    <input name="Zip" type="text" id="Zip" maxlength="5" />
    </label>
    </form>

    <form id="form8" name="form8" method="post" action="">
    <label>Submit
    <input type="submit" name="Submit" id="Submit" value="Submit" />
    </label>
    </form>


    Your Birth Date:
    <input type=text name=date size=10 maxlength=10> (in MM/DD/YYYY format)
    <input type=submit value="Submit Date">
    </form>
    </body>
    </html>

    Any help will be appreciated.
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    No one will help you until you read the posting guidelines.

    Posting Guidelines

    Comment

    • Theadmin77
      New Member
      • Nov 2006
      • 19

      #3
      Sorry , i will post it again !!!

      Comment

      • Theadmin77
        New Member
        • Nov 2006
        • 19

        #4
        Problems with validation inputs!!

        I have to create a site that have fields to put the following info and input must be validated and error displayed otherwise.

        * First Name (limit 25 characters)
        * Last Name (limit 25 characters)
        * Street Address (limit 45 characters)
        * City (limit 25 characters)
        * State (2 character code )
        * Zip Code (5 digits only, must be numeric)
        * Date of Birth (mm/dd/yyyy format, day, month, and year must be valid)

        I have the site created but the validation is not working
        THE RELEVANT PART OF THE CODE IS HERE:

        <script language=JavaSc ript src="mycheck.js " type="text/javascript"> </script>
        </head>

        <body>
        <form name="form1" method="post" action="" onSubmit="Valid ation(this)">

        <p>First:
        <input type="text" name="fname" maxlength="25" size="25"></p>

        <p>Last:
        <input type="text" name="lname" maxlength="25" size="25"></p>
        <p>Address:
        <input type="text" name="address" maxlength="45" size="45"></p>
        <p>City:
        <input type="text" name="city" maxlength="25" size="25"></p>
        <p>State:
        <select name="state">
        <option value=""></option>
        <option value="AL">Alab ama</option>
        <option value="AK">Alas ka</option>
        <option value="AZ">Ariz ona</option>
        <option value="AR">Arka nsas</option>
        <option value="CA">Cali fornia</option>
        <option value="CO">Colo rado</option>
        <option value="CT">Conn ecticut</option>
        <option value="DE">Dela ware</option>
        <option value="DC">Dist rict Of Columbia</option>
        <option value="FL">Flor ida</option>

        </select></p>

        <p>Zip:
        <input type="text" name="zipcode" maxlength="5" size="5"></p>

        <p>Date of Birth:
        <input type="text" name="txtDate" maxlength="10" size="10"></p>
        <p><input type="submit" name="Submit" value="Submit"> </p>
        </form>

        </body>

        THIS IS MYCHECK.JS FILE:

        SCRIPT LANGUAGE="JavaS cript">
        function isValidDate(dat eStr) {

        var datePat = /^(\d{1,2})(\/|)(\d{1,2})\2(\ d{4})$/;


        var matchArray = dateStr.match(d atePat);

        if (matchArray == null)
        {
        alert("Null entry date.")
        return false;
        }

        month = matchArray[1];
        day = matchArray[3];
        year = matchArray[4];

        if (month < 1 || month > 12)
        {
        alert("Wrong entry , 1 and 12 value needed!!!");
        return false;
        }
        if (day < 1 || day > 31)
        {
        alert("Wrong entry 1 and 31 value needed !!!");
        return false;
        }
        if ((month==4 || month==6 || month==9 || month==11) && day==31)
        {
        alert("Wrong entry, 31 days needed!")
        return false
        }
        if (month == 2)
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day>29 || (day==29 && !isleap)) {
        alert("Wrong entry ,February!!!");
        return false;
        }
        }
        return true;
        }

        // End -->
        </script>


        Any help will be appreciated.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Remember: Javascript isn't Java. I'll move this to the appropriate Javascript forum.

          kind regards,

          Jos

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            I just moved your other thread to the Javascript forum where it belongs. Java is
            not Javascript and this forum is a Java forum. We can't help you here. I'll close
            this thread; please continue your discussion in the Javascript forum where you'll
            find your other thread again. Thanks.

            kind regards,

            Jos

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Changed thread title.

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                You've got 8/9 forms - you only need one. Get rid of all the extra form tags.

                Also, read up on validation by checking out the Form validation links in the Offsite Links thread at the top of the Javascript forum.

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Threads merged.

                  Comment

                  Working...