Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Regular Expressions - Page 5

September 5, 2000

That really was not too bad, but it seems fairly tedious to go through every character with a for...next loop. That is where Regular Expressions come in. If you can get set up and working properly, then they are definitely the way to go, and you will see why in a minute.

For starters, I recommend you check out Microsoft's VBScript Regular Expression Introduction, if you are brand new to the technique (or if you are a very Perl oriented individual). Once you've glanced through that and printed out the Regular Expression Syntax page, study the following code that performs the same thing as our for...next loop above. In fact, this particular Regular Expression kills two birds with one stone by eliminating the need to check for a blank field!

<%
	...
	dim re, results
	set re = New RegExp
	re.Pattern = "^\D+$"
	re.Global = True
	re.IgnoreCase = True
	results = re.Test(first_name)
	if results then
		errorArray(0) = "False"
	else
		errorArray(0) = "True"
		ErrorMsg = "First Name<br>"
	end if
	...
%>

To begin, you must declare the variables re and results (you can name them whatever you want). Then, we invoke the RegExp object in the form of an instance, re.

Following that, it is important to set some parameters for the regular expression. Most importantly is the pattern that will be matched, which is, ^\D+$. If you check out that reference sheet I asked you to print, you will note that \D stands for any non-digit character, while the + tells the Regular Expression to match one or more of these characters. The ^ and $ makes sure it checks and is limited to the whole string. Granted, that means it will not pick up symbols, but that is not necessarily a bad thing if a foreign name is possible. However, if you want to be thorough, try this: re.Pattern = "^[^0-9\/><\.,\\!\^\$\*\+\?@#%&\(\);:\[\]\{\}=""']+$". Yes, that is frightening, but all it says is not (^) 0-9 or any of the symbols following. If there is a backslash before a symbol, that means it has been "escaped." Symbols that represent a behavior in a Reqular Expression must first be escaped if you want to search for them. Whichever you decide to use, if results is found to be true (and the pattern is matched), then there is no error, else there is and we note that with our array and ErrorMsg.

As you can see, while a bit more complex, this is very effective at finding patterns in a string. This feature will make itself more evident when we get into validating specific patterns like social security numbers, telephone numbers, etc. on the next page. If you would like to see Regular Expressions in action, visit this other sample at Enfused. Also, just for your convenience, here is what the code will look like in the whole scheme of things:

<%
	const numFields = 9
	dim errorArray()
	redim preserve errorArray(numFields)

	if request.form("isSubmitted") = "yes" then
		...
		...
		ErrorMsg = ""
		dim re, results
		set re = New RegExp
		re.Pattern = "^[^0-9\/><\.,\\!\^\$\*\+\?@#%&\(\);:\[\]\{\}=""']+$"
		re.Global = True
		re.IgnoreCase = True
		results = re.Test(first_name)
		if results then
			errorArray(0) = "False"
		else
			errorArray(0) = "True"
			ErrorMsg = "First Name<br>"
		end if
		if Len(last_name) = 0 then
			errorArray(1) = "True"
			ErrorMsg = ErrorMsg & "Last Name<br>"
			'Note, I added ErrorMsg & to this line to make 
				'sure we keep the whole string
		end if
	end if
%>

I do not want to overload you in this tutorial, so on the next page I will guide you through a few more regular expressions for common fields and then we will call it quits so you can go practice.

Making Sure People Do Not Enter L33t As Their Name - Page 4
Using ASP for Form Handling: Part 2 - Server Side Form-Field Validation
How To Deal With Specific Sequences of Numbers and Characters - Page 6


Up to => Home / Authoring / ASP / FormHandling




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers