Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript Regular Expression Help Needed

Status
Not open for further replies.

dwest100

Technical User
Aug 9, 2003
59
US
Hi,
I have the following e-mail validation code:
Code:
fValidate.prototype.email = function( level )
{
	if ( this.typeMismatch( 'text' ) ) return;
	if ( typeof level == 'undefined' ) level = 0;
	var emailPatterns = [
        /.+@.+\..+$/i,
		/^\w.+@\w.+\.[a-z]+$/i,
		/^\w[-_a-z~.]+@\w[-_a-z~.]+\.[a-z]{2}[a-z]*$/i,
		/^\w[\w\d]+(\.[\w\d]+)*@\w[\w\d]+(\.[\w\d]+)*\.[a-z]{2,7}$/i,
		];
	if ( ! emailPatterns[level].test( this.elem.value ) )
	{
		this.throwError();
	}	
}

It works fine but it does not validate ".con" as incorrect for ".com"

So, I found this regular expression which does validate for .com, .gov etc.:
Code:
/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi
However, if I simply replace the regular expressions in the first code sample with the second code sample, the first code sample breaks.

My question is how can I incorporate the extension checking capability of the second sample in to the first code sample?

Thanks much!
 

Would that validation routine work for addresses that don't end in 3 digit or 2,2 digit extensions? If not - consider domains things like ".tv", etc, too.

Hope this helps,
Dan
 
Thanks. What I'm asking is how to incorporate all of those into what exists above. I don't know anything about reg ex.

Everything I've found on the web stops at the 2 digit, 3 digit mentality and considers it sufficient validation.

But that's not validated is it? After all one can't consider abc@def.con a valid e-mail address since it would not find a destination due to the mis-typing of the "m" at the end.

Any ideas?? :)
 
Well, as it turns out I just added the second code sample to the bottom of the first like so:
Code:
fValidate.prototype.email = function( level )
{
	if ( this.typeMismatch( 'text' ) ) return;
	if ( typeof level == 'undefined' ) level = 0;
	var emailPatterns = [
        /.+@.+\..+$/i,
		/^\w.+@\w.+\.[a-z]+$/i,
		/^\w[-_a-z~.]+@\w[-_a-z~.]+\.[a-z]{2}[a-z]*$/i,
		/^\w[\w\d]+(\.[\w\d]+)*@\w[\w\d]+(\.[\w\d]+)*\.[a-z]{2,7}$/i,
        /\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi,
		];
	if ( ! emailPatterns[level].test( this.elem.value ) )
	{
		this.throwError();
	}	
}
And it works like a charm.

The first sample is set up so each filter is a "level" of strictness. So I set the strictness to level 4 after adding the last filter.

Don't know why but it works. ;-)

I just didn't want anyone wasting any time on this thread since I figured it out.

Thanks!!
 
Thanks!
I'll be sure to include them as well.
Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top