SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    May 2001
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    form validation problems.

    Hey,
    I am using the following javascript to validate the data entered:

    Code:
    <script type="text/javascript">
    function y2k(number) { return (number < 1000) ? number + 1900 : number; }
    var reason = '';
    function isValidDate (myDate,sep) {
    // checks if date passed is in valid dd/mm/yyyy format
        if (myDate.length == 10) {
            if (myDate.substring(2,3) == sep && myDate.substring(5,6) == sep) {
                var date  = myDate.substring(0,2);
                var month = myDate.substring(3,5);
                var year  = myDate.substring(6,10);
                var test = new Date(year,month-1,date);
                if (year == y2k(test.getYear()) && (month-1 == test.getMonth()) && (date == test.getDate())) {
                    reason = '';
                    return true;
                }
                else {
                    reason = 'valid format but an invalid date';
                    return false;
                }
            }
            else {
                reason = 'invalid spearators';
                return false;
            }
        }
        else {
            reason = 'invalid length';
            return false;
        }
    }
    function isDate(aTextField) {
        myDate = document.forms[0][aTextField].value
        if (isValidDate(myDate,'/'))
         return false;
        else
            return true;
    }
    function setFocus(aField) {
    document.forms[0][aField].focus();
    }
    function isAnEmailAddress(aTextField) {
    if (document.forms[0][aTextField].value.length<5) {
    return false;
    }
    else if (document.forms[0][aTextField].value.indexOf("@") < 1) {
    return false;
    }
    else if (document.forms[0][aTextField].value.length - document.forms[0][aTextField].value.indexOf("@") < 4) {
    return false;
    }
    else { return true; }
    }
    function isEmpty(aTextField) {
    if ((document.forms[0][aTextField].value.length==0) || (document.forms[0][aTextField].value==null)|| (document.forms[0][aTextField].value==0)) {
    return true;
    }
    else { return false; }
    }
    function isDifferent(aTextField, bTextField) {
    if ((document.forms[0][aTextField].value!=document.forms[0][bTextField].value)) {
    return true;
    }
    else { return false; }
    }
    function isCheckBoxEmpty(aCheckBox) {
    if (document.forms[0][aCheckBox].checked==false) {
    return true;
    }
    else { return false; }
    }
    function validate() {
    // check that the name field is valued
    if (isEmpty("Fields[Region]")) {
     alert("Region is a required field.");
     setFocus("Fields[Region]");
     return false;
    }
    if (isEmpty("Fields[Postcode]")) {
     alert("Postcode is a required field.");
     setFocus("Fields[Postcode]");
     return false;
    }
    if (isEmpty("Fields[Email]")) {
     alert("Email is a required field.");
     setFocus("Fields[Email]");
     return false;
    }
    if (isEmpty("Fields[EmailVerify]")) {
     alert("Email Verify is a required field.");
     setFocus("Fields[EmailVerify]");
     return false;
    }
    if (isDifferent("Fields[Email]","Fields[EmailVerify]")) {
     alert("The two email addresses are different.");
     setFocus("Fields[Email]");
     return false;
    }
    if (isEmpty("Fields[U_Password]")) {
     alert("Password is a required field.");
     setFocus("Fields[U_Password]");
     return false;
    }
    if (isEmpty("Fields[U_PasswordVerify]")) {
     alert("Password Verify is a required field.");
     setFocus("Fields[U_PasswordVerify]");
     return false;
    }
    if (isDifferent("Fields[U_Password]","Fields[U_PasswordVerify]")) {
     alert("The passwords do not match.");
     setFocus("Fields[U_Password]");
     return false;
    }
    if (isEmpty("Fields[DateOfBirth]")) {
     alert("Date Of Birth is a required field.");
     setFocus("Fields[DateOfBirth]");
     return false;
    }
    if (isDate("Fields[DateOfBirth]")) {
     alert("Date of Birth is invalid.");
     setFocus("Fields[DateOfBirth]");
     return false;
    }
    if (isCheckBoxEmpty("agree")) {
     alert("You need to agree to the Terms of Engagement.");
     return false;
    }
    // if we get this far everthing is ok, so
    // let the form submit
    return true;
    }
    </script>
    It seems to work with some strange quirks. For example -

    1. When the 2 email addresses or passwords dont match, it gives the error that they dont match, but then continues to submit the form. It should not do this.

    2. How does one check if an option has been selected from a drop down menu?

    thanks
    unregistered1.

  2. #2
    SitePoint Addict
    Join Date
    May 2001
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can anyone help me?

  3. #3
    SitePoint Addict jtrelfa's Avatar
    Join Date
    Oct 2004
    Location
    Troy, Mi
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What does your html look like? is this an onsubmit event or an onclick for the submit button?

    For a select box, you can use the selectedIndex property:
    Code:
    var sel = document.forms[0]["selectBox"];
    var selected = sel[sel.selectedIndex].value;

  4. #4
    SitePoint Addict
    Join Date
    May 2001
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its on submit function.
    Code:
    <form action=signup_submit.php method=post onsubmit="return validate()">

  5. #5
    SitePoint Addict
    Join Date
    May 2001
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    anyone?

  6. #6
    SitePoint Addict jtrelfa's Avatar
    Join Date
    Oct 2004
    Location
    Troy, Mi
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not seeing where your issue is coming from. I can see that you're doing an appropriate check for the comparison of email address values being equal, but I don't see how it's failing or why it's allowing the submit.

    Can you post a URL or the complete code for review?

  7. #7
    SitePoint Addict
    Join Date
    May 2001
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i figured out the problem. the field names were different.

    thank you everyone.


Bookmarks

Posting Permissions

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