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!
  • Students Click Here

*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.

Students Click Here



Another Regular Expression Based Validation by Tarwn
Posted: 27 Aug 03

I wrote this in response to a thread in one of the other forums and felt I would clean it up and post it here. Basically the way this works is that for any input that you would like validated you simply specify a pattern that the value should match against and an option error message that should be displayd should the input not pass validation. Currently this only handles text inputs, but could be easily extended for other cases as well:
<script language="Javascript">
    //verify function to verify all inputs with a set pattern
    function verify(frmElem){
        var regex, pattern, errmsg;
        var result;
        //loop through all form elements
        for(var i = 0;i < frmElem.length;i++){
            //try to grab the pattern
            pattern = frmElem.elements(i).testcode;
            //make sure there was a pattern
                //create a regexp object with the specified pattern
                regex = new RegExp(frmElem.elements(i).testcode,"i");
                //test the value with the supplied pattern

                    //oops, incorrect entry, get the error message
                    errmsg = frmElem.elements(i).testerror;

                    //if there wasn't one, make one up
                        errmsg = "Please double check the selected entry, it is incorrectly formated."

                    //do a std error reaction
                    return false;
        return true;

<!-- Some sample code -->

<form method="POST" action="sample.asp" onSubmit="return verify(this);">

Enter a Number: <input type="text" name="txtNum" testcode="^\d$" testerror="You may only enter a single number in the first input.">*<br>

Enter a Letter: <input type="text" name="txtLetter" testcode="^[a-zA-Z]$" testerror="You may only enter a single letter in the second input.">*<br>

Enter whatever you want: <input type="text" name="txtAnything"><br>

Enter a Phone Number: <input type="text" name="txtPhone" testcode="^\d{3}-\d{3}-\d{4}$" testerror="Please check that your phone number is in the correct format (###-###-####).">*<br>

Enter an Email: <input type="text" name="txtEmail" testcode="^[\w-_\.]+\@[\w-_]+(\.[\w-_]+)+$" testerror="Please check that your email is formatted correctly.">*<br>

<input type="submit" value="Submit">

Basically what happens is that the script loops through all of the form elements in the form you wish to verify. With each element it attempts to get the testcode (pattern) you wish to use for validation. If the pattern exists than the script will test the value of the input to make sure it matches the pattern. Should it not match the script then attempts to get a specified error message from that input, falling back on a generic error message should one not be available.

The script itself is relatively short, but from messing with it a litle more I can see how it could be easily extended with a switch statement on the input type in order to test for differant conditions based on the type of input that is to be checked.

Back to Javascript FAQ Index
Back to Javascript Forum

My Archive

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