To read the article online, visit

Validate What? Using Regular Expressions to Validate Input
By Ian Stallings

Updated Regular Expression
Ian has an updated version of this function at

Validating user input is a basic but very necessary part of web development, especially when dealing with databases. Queries don't go too well when you have !@)#&#$*%?= in the search field. So we have to build a function that searches the input string for illegal characters (defined by you) and then let the user know.

If you've used Asp's before you probably know that you can use the following vbscript to validate input:

If (InStr(request.form("password"),"!") > 0) Then
    response.write "Username cannot contain odd characters"
End If

But what about multiple characters? We could use the above vbscript but it's not very efficient when dealing with multiple characters. I like to use Javacript as the language of choice because of it's ability to use regular expressions.

So what are regular expressions? Regular expressions are used in pattern matching and substitution. The regular expression interpreter takes your grammar, compares it with the string you're doing pattern matching on, then returns a true or false. True meaning it found a match, false meaning it didn't.

So, now that we've covered the basics let's dig into the code. The following javascript function will validate the string:

<script language=javascript runat=server>

function inStrGrp(src,reg) {
    var regex=new RegExp("[" + reg + "]","i");
    return regex.test(src);


You would then call this from your vbscript code using the following:

If inStrGrp(request.form("password"),"!,@#%$^&*()+=") = True Then
    response.write "Password cannot contain odd characters"
end if

And that is it. Not 100 lines of code, just 10. You can add characters to the expression if you need to. If you have any comments or questions feel free to email me at!

Article Information
Article Title: Validate What? Using Regular Expressions to Validate Input
Article Author: Ian Stallings
Published Date: Monday, May 03, 1999
Article URL:

Copyright 2017 QuinStreet Inc. All Rights Reserved.
Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers