Form Validation

<SCRIPT>

/********** Email Check **********/

function validEmail(email) {
     var invalidChars = " /:,;"; // NOTE - first char is a SPACE

     for (i = 0;  i < invalidChars.length; i++) { // does it contain any invalid characters?
          var badChar = invalidChars.charAt(i);
          if (email.indexOf(badChar, 0) > -1) return false;
     }

     var atPos = email.indexOf("@", 1);  // there must be one "@" symbol
     if (atPos == -1) return false;
     // and only one "@" symbol
     if (email.indexOf("@", atPos + 1) != -1) return false;

     periodPos = email.indexOf(".", atPos);
     // and at least one "." after the "@"
     if (periodPos == -1) return false;
     // must be at least 2 characters after the "."
     if (email.lastIndexOf(".") + 3 > email.length) return false;
     if (email.length > email.lastIndexOf(".") + 4) return false;

     return true;
}

/********** End of Email Check **********/

/********** Zip Code Check **********/

function validZip(Zip) {
     if ((Zip.length == 5) && (!isNaN(Zip))) return true; // checking for 5 digit Zip
     else if ((Zip.length > 5) && (Zip.length <= 10)) { // checking for 9 digit Zip
          if (Zip.indexOf("-", 0) > -1) { // split Zip if it contains a "-" into multiple array elements
               var Zips = new Array();
               Zips = Zip.split("-");
               if (Zips[1] == null) return false; // making sure that 2nd array is not empty, the last digits
               // making sure that both array elements are only numbers &
               // that the 2nd array element is only 4 digits

               if ((!isNaN(Zips[0]) && !isNaN(Zips[1])) && (Zips[1].length == 4)) return true;
               else return false;
          }
          else return false;
     }
     else return false;
}

/********** End of Zip Code Check **********/

/********** Is it a Number Check **********/

function isNum(num) {
     var numInvalid = 0;

     for (i = 0; i < num.length ; i++) {
          if (!((num.charAt(i) >= "0") && (num.charAt(i) <= "9"))) numInvalid++;
     }

     return (numInvalid > 0) ? false : true;
}

/********** End of Is it a Number Check **********/

/********** "Alpha" Character Check **********/

function alpha(str) {
     var strInvalid = 0;

     for (i = 0; i < str.length; i++) {
          if (
!(((str.charAt(i) >= "A") && (str.charAt(i) <= "Z")) ||
                 ((str.
charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
                 (str.
charAt(i) == "'") || (str.charAt(i) == " ") ||
                 (str.
charAt(i) == "-") || (str.charAt(i) == "."))) strInvalid++;
     }

     return (strInvalid > 0) ? false : true;
}

/********** End of "Alpha" Check **********/

/********** Main Validation Checking **********/

function ValidateInput(form) {
    var LB = "\n";
    var msghdr = "Please fill out your:" + LB + LB;
    var msgftr = LB + "Please complete and try again.";
    var msg = "";

    if (!form.First.value) msg += "First Name" + LB;
    else if (!alpha(form.First.value)) msg += "Invalid First Name" + LB;

    if (!form.Last.value) msg += "Last Name" + LB;
    else if (!alpha(form.Last.value)) msg += "Invalid Last Name" + LB;

    if (!form.Address.value) msg += "Address" + LB;

    if (!form.City.value) msg += "City" + LB;
    else if (!alpha(form.City.value)) msg += "Invalid City Name" + LB;

    if (!form.State.selectedIndex) msg += "State" + LB;

    if (!form.Zip.value) msg += "Zip Code" + LB;
    else if (!validZip(form.Zip.value)) msg += "Invalid Zip Code" + LB;

    if (!form.Age.value) msg += "Age" + LB;
    else {
        // if (!isNum(form.Age.value)) msg += "Invalid Age" + LB;
        if (isNaN(form.Age.value)) msg += "Invalid Age" + LB;
        else if (form.Age.value <= 0 || form.Age.value > 100) {
            msg += " Age should be between 1 and 100" + LB;
        }
    }

    if (!form.Income.selectedIndex) msg += "Income" + LB;

    if (!form.Sex[0].checked && !form.Sex[1].checked) msg += "Sex" + LB;

    // if email address is empty
    if (!form.Email.value) msg += "Email Address" + LB
    // else if the email address is invalid
    else if (!validEmail(form.Email.value)) msg += "Please correct your email address." + LB

    // Display an alert if any of the input is missing:
    if (msg.length > 0) {
        alert(msghdr + msg + msgftr);
        return false;
    }
    else return true;
}

// For FormMail.pl - one possible solution

if (msg.length > 0) {
    
alert(msghdr + msg + msgftr);
     return false;
}
else {
     form.recipient.value = form.Email.value;
     return true;
}

/********** End of Validation Checking **********/

</SCRIPT>

<FORM METHOD="POST" ACTION="jmail.asp" onSubmit="return ValidateInput(this)">