INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Validation

Validation using Object-Oriented Patterns by Glowball
Posted: 14 Oct 01

This external Javascript (.js) file can be used as one form validator for an entire site.  To use it correctly you should follow these rules:

1.  For good error messages, name your form fields descriptive names, with an underscore (_) where a space would go.  "First_Name" is a good example.

2.  Create a folder off of root named "scripts" to house all of your external Javascript files, including this one.

3.  For form fields that you want validated, use the parameter VALIDATOR.  This must be in all capital letters!  More details will follow.

4.  Use the patterns provided or supply your own.  VALIDATOR="notEmptyPat" will simply check for any value at all.  The others will match patterns in the user's entry for that field.

---------------------------------------
YOUR FILE
---------------------------------------

<html>
<head>
<script language="JavaScript" src="/scripts/validate.js" type="text/javascript"></script>
</head>
<body>

<form name="myForm" id="myForm" onSubmit="return validate(myForm)">
Name: <input type=text name="Name"
VALIDATOR="notEmptyPat"><br>
Email Address: <input type=text name="Email_Address"
VALIDATOR="emailPat"><br>
<input type=submit value="Submit">
</form>

</body>
</html>

---------------------------------------
EXTERNAL JAVASCRIPT FILE (validate.js)
---------------------------------------

function replace(haystack, oldNeedle, newNeedle) {
    i = haystack.indexOf(oldNeedle);
    r = "";
    if (i == -1) return haystack;
    r += haystack.substring(0,i) + newNeedle;
    if (i + oldNeedle.length < haystack.length)
        r += replace(haystack.substring(i + oldNeedle.length, haystack.length), oldNeedle, newNeedle);
    return r;
    }

function validate(objForm) {
    var PatternsDict = new Object();
    PatternsDict.zipPat = /\d{5}(-\d{4})?/;  // matches zip codes
    PatternsDict.
emailPat = /.*@.*\..*/;  // matches email addresses
    PatternsDict.
notEmptyPat = /.{1,}/;  // matches at least one character
    PatternsDict.numberPat = /\d/;  // matches numbers only
    PatternsDict.pwPat = /^\D{1}\S{3,9}$/;  // matches between 4 and 10 characters with non-digit leading
    PatternsDict.currencyPat = /\$\d{1,3}(,\d{3})*\.\d{2}/;  // matches currency with commas
    PatternsDict.timePat = /^([1-9]|1[0-2]):[0-5]\d$/;  // matches times

    var elArr = objForm.elements;
    for(var i=0; i<elArr.length; i++)
    with(elArr[i]) {
        var v = elArr[i].VALIDATOR;
        if(!v) continue;
        
        var thePat = PatternsDict[v];
        var gotIt = thePat.exec(value);
        if(!gotIt) {
            var returnStr;
            readName = replace(name, "_", " ");
            returnStr = "The " + readName + " field is invalid, this field is required in order to submit this form.  Please try again!";
            alert(returnStr);
            return false;
            }
        }
    return true;
    }

Back to Javascript FAQ Index
Back to Javascript Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close