[Home]Validate forms

HomePage | RecentChanges | Preferences | Newbie Help

[Category JavaScript FAQ]?


Comments --

Joe Alexander said on 15 May 2001 on alt.html --

//--------------------javscript debug-------------------------

function reportError(msg,url,line)
    {
        var str = "Err: " + msg + " on line: "
        + line + "\nURL: " + url  + "\nWeb: "
        + navigator.appName + " " + navigator.appName
        + " " + navigator.appVersion;
        document.errform.error.value = str;
        return true;
    }
window.onerror = reportError;

//--------------------validate first form---------------------

function checkform2(form)
    {
        if (form.fname.value == ''||form.fname.value == '*required*')
            {
                alert ("Please Enter Your First Name.\nThank you");
                form.fname.focus();
                form.fname.value = '*required*';
                form.fname.select();
                return false;
            }

        if (form.lname.value == ''||form.lname.value == '*required*')
            {
                alert ("Please Enter Your Last Name.\nThank you");
                form.lname.focus();
                form.lname.value = '*required*';
                form.lname.select();
                return false;
            }

        if (form.addr.value == ''||form.addr.value == '*required*')
            {
                alert ("Please Enter Your Shipping Address.\nThank you");
                form.addr.focus();
                form.addr.value = '*required*';
                form.addr.select();
                return false;
            }


        if (form.city.value == ''||form.city.value == '*required*')
            {
                alert ("Please Enter Your City.\nThank you");
                form.city.focus();
                form.city.value = '*required*';
                form.city.select();
                return false;
            }
       if (form.state.options[form.state.selectedIndex].text == "--Select
One--")
            {
                alert ("Please  Select A State.\nThank you");
                form.state.focus()
                retval=false;
                return retval;
            }
       if (form.state.options[form.state.selectedIndex].text == "Other" &&
(form.stateother.value == 'Other State/Province' || form.state.value ==
'*required*'))
            {
                alert ("Please  Enter Your State/Province.\nThank you");
                form.stateother.focus()
                form.stateother.value = '*required*';
                form.stateother.select();
                retval=false;
                return retval;
            }

        if (form.zip.value == ''||form.zip.value == '*required*')
            {
                alert ("Please Enter Your Postal Code.\nThank you");
                form.zip.focus();
                form.zip.value = '*required*';
                form.zip.select();
                return false;
            }

       if (form.cctype.options[form.cctype.selectedIndex].text == "Pick One")
            {
                alert ("Please  Select A Credit Card Type.\nThank you");
                form.cctype.focus()
                retval=false;
                return retval;
            }

        if (form.ccexpmo.options[form.ccexpmo.selectedIndex].text == "Pick
One")
            {
                alert ("Please  Select Your Expiration Month.\nThank you");
                form.ccexpmo.focus()
                retval=false;
                return retval;
            }

        if (form.ccexpyr.options[form.ccexpyr.selectedIndex].text == "Pick
One")
            {
                alert ("Please  Select Your Expiration Year.\nThank you");
                form.ccexpyr.focus()
                retval=false;
                return retval;
            }

        if (form.ccnum.value == '' || form.ccnum.value.length < 14 ||
form.ccnum.value == '*required*')
            {
                alert ("Please A Valid Credit Card Number.\nThank you");
                form.ccnum.focus();
                form.ccnum.value = '*required*';
                form.ccnum.select();
                return false;
            }

        return emailCheck(form.email.value);
    }

function emailCheck (emailStr)
    {
        var emailPat=/(.+)@(.+)/;
        var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
        var validChars="\[^\\s" + specialChars + "\]"
        var quotedUser="(\"[^\"]*\")";
        var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
        var atom=validChars + '+';
        var word="(" + atom + "|" + quotedUser + ")";
        var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
        var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
        var matchArray=emailStr.match(emailPat);
        if (matchArray==null)
            {
             alert("Email address seems incorrect (check @ and .'s)");
             return false;
            }
        var user=matchArray[1]
        var domain=matchArray[2]

        if (user.match(userPat)==null)
            {
                alert("The email username doesn't seem to be valid.");
                return false;
            }

        var IPArray=domain.match(ipDomainPat);
        if (IPArray!=null)
            {
             for (var i=1;i<=4;i++)
                 {
                     if (IPArray[i]>255)
                         {
                             alert("Email Destination IP address is invalid!");

                          return false;
                         }
                    }
                //return true;
            }

        var domainArray=domain.match(domainPat)
        if (domainArray==null)
            {
             alert("The email domain name doesn't seem to be valid.");
                return false;
            }

        var atomPat=new RegExp(atom,"g");
        var domArr=domain.match(atomPat);
        var len=domArr.length;
        if (domArr[domArr.length-1].length<2 ||
domArr[domArr.length-1].length>3)
            {
                alert("The email address must end in a three-letter domain, or
two letter country.");
                return false;
            }

        if (len<2)
            {
                var errStr="This email address is missing a hostname!";
                alert(errStr);
                return false;
            }

        return true;
    }

HomePage | RecentChanges | Preferences | Newbie Help
This page is read-only | View other revisions
Last edited May 17, 2001 11:53 am (diff)
Search:

This FAQ is happily hosted by Betadome Digital Media