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


How To Deal With Specific Sequences of Numbers and Characters - Page 6

September 5, 2000

This page will focus primarily on validating all the other fields we have somewhat ignored on the previous page, like Social Security, Telephone, Zip Code and the Email Address. Since this article is getting pretty long, I will spend less time explaining and more time showing; however, you should walk away with a lot of useful code and understanding for your own web projects.

Social Security Numbers

To validate a social security field you have to first conceptualize how people are going to enter the data. Most people will probably enter 111-11-1111 or 111111111. Now, you can allow either to be valid or force a specific format. I recommend that if you force a specific format, just make sure you tell your users. Below is a good pattern to use assuming that you are forcing 111-11-1111. It checks for 3 digits (\d{3}), a dash, 2 digits, a dash, and 4 digits. Just insert this code after your code for checking the Last Name.

<%
	...
	re.Pattern = "^\d{3}-\d{2}-\d{4}$"
	results = re.Test(social_security)
	if results then
		errorArray(2) = "False"
	else
		errorArray(2) = "True"
		ErrorMsg = ErrorMsg & "Social Security<br>"
	end if
	...
%>

Zip Codes

Assuming you are in the US, zip codes now have a special format and look like: 11111-1111. However, not all people know the four digit extension; so you had better be sure to make your code accepting of both types. I recommend the following pattern shown in the code below. It is a bit trickier then the other ones we have used so far, so pull out your reference sheet again. \d{5} checks for the first five digits, which is common to both zip codes, whether the user knows the four digit extension or not. The next part in parenthesis is what one would call a nested pattern. (-\d{4})? checks for the dash and four digit extension zero or one times (the question mark ? takes care of that). That is all there is too it!

<%
	...
	re.Pattern = "^\d{5}(-\d{4})?$"
	results = re.Test(zip_code)
	if results then
		errorArray(6) = "False"
	else
		errorArray(6) = "True"
		ErrorMsg = ErrorMsg & "Zip Code<br>"
	end if
	...
%>

Telephone Numbers

Validating a telephone number can get tricky, because there are many ways to properly enter a telephone number. Therefore, for this article, we will force a very normal format of ###-####. If you want to be more accepting, use your imagination and reference sheet to create a more complex pattern to match. Notice how it is not unlike the social security number. Tests for 3 digits, a dash and then 4 digits. Make sure for all these tests that you keep those ^ and $ in there, those will ensure that the full string is matched and no extraneous characters are ignored. (For example, if someone enters 240-4334-343444, that will validate without the $, but if you put the $ in, it will return an error).

<%
	...
	re.Pattern = "^\d{3}-\d{4}$"
	results = re.Test(telephone)
	if results then
		errorArray(7) = "False"
	else
		errorArray(7) = "True"
		ErrorMsg = ErrorMsg & "Telephone Number<br>"
	end if
	...
%>

Email Addresses

If you had hoped you would learn how to get a user to give you a real and totally valid email address, I am sorry to dash your hopes, but that will not happen here. A lot can be done to attempt to validate an email address, but it takes some extremely complex matching and you can never be one hundred percent accurate. The following pattern is fairly basic but will suffice for most situations. First, think of the things that are standard to email addresses. There is the @ sign, and a ., that is about it. So, we will work around those to come to a usable solution.

<%
	...
	re.Pattern = "^\w+@\w+\.\w+"
	results = re.Test(email)
	if results then
		errorArray(8) = "False"
	else
		errorArray(8) = "True"
		ErrorMsg = ErrorMsg & "Email Address<br>"
	end if
	...
%>

As you can see, it is not a hugely complex pattern, but it gets the job done. It checks for "word" (A-Za-z0-9_) characters (any number of them, but at least one), the @ sign, any number of word characters, the . (escaped by a backslash \.) and then some more word characters. Make sure you drop the $ from the end, because some email addresses end in something like mail.cc.foo.com. Once you get the hang of this stuff, it will come much easier to you and you can do a great many things. Check out the Enfused sample page to see all of this stuff in action.

Regular Expressions - Page 5
Using ASP for Form Handling: Part 2 - Server Side Form-Field Validation
A Final Look at the Code - Page 7


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