JS: Please critique this email-address-checking script

by Complex <complex_hwg(at)yahoo.com>

 Date:  Tue, 14 May 2002 15:56:19 -0700 (PDT)
 To:  Languages HWG <hwg-languages(at)hwg.org>
  todo: View Thread, Original
Please critique this email-address-checking JavaScript. This is the
client-side pre-checker; I intend to have a matching script on the
server checking the submitted form.

I breezed through the O'Reilly JavaScript section on regular
expressions and assembled it. I've done some testing on it, but I'm
hoping you know some better rocks to hurl at it.

This JavaScript is meant to check the rough shape of the submitted
email address. The main problem it seeks to solve is our sending
replies to bad email addresses.
I couldn't find really good information on rules for acceptable email
addresses, particularly on the local part. I think I did a fair job of
matching the pattern for the domain part.

			function isEmailAddr(Email_Address)
			{
				var result = false;
				
				//  Next line works only in JavaScript >= 1.2!
				var emailPat = /^\w[-_\.\w]*@(\w|\w[-\.\w]*\w)\.[a-zA-Z]{2,4}$/;
				var emailStr = new String(Email_Address);
				alert("The function isEmailAddr says: " + emailPat.test(emailStr));
				
			}

You can see the form (and the source) in action at:

http://www.scanalytics.com/webtest/downloadEntryC.html  (corporate test
page)

After you click Submit, an alert should tell you True for good emails.
The form'll fail anyway to stop the tests from getting through.

One problem I see is that expression matching (apparently) only works 
in JS1.2 and up. I don't think that's a problem for my customer base,
but I don't know how to make it degrade gracefully.  I suppose adding
"language='JavaScript1.2'" would help, but that's been deprecated.
Should I use it anyway, or is there another way?

Also, anyone know how to make that expression more readable?  :-)

many thanks,
c

__________________________________________________
Do You Yahoo!?
LAUNCH - Your Yahoo! Music Experience
http://launch.yahoo.com

HWG: hwg-languages mailing list archives, maintained by Webmasters @ IWA