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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Email validation

Status
Not open for further replies.

4345487

Programmer
Dec 31, 2002
132
US


Hi,

Maybe someone can help me with this email validation

alert( ((/\w+((-\w+)|(\.\w+)|(\_\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z]{2,4}/).test(email)));

I want the values after the dot to no less that 2 and no more that 4 so I use {2,4} if I enter test@test.c it returns false which is what I spected but if I enter test$test.cmmmmmmmmm it returns true what i'm doing wrong.

Thanks in advance




 
This is the function I use.
Code:
function validEmail(address) {
	if (address) {
		if (address.match(/^[^@]+@[^@]+\.[A-Za-z]{2,4}$/)) {
			return true;
		}
	}
}

M. Brooks
 
what i'm doing wrong

You're not checking for the end of the string. Your regexp looks to see if there's between 2 and 4 characters after the decimal (which there is). The problem is that it just ignores the rest of the stuff afterwards. Use the following 2 characters to check for the beginning and end of the string:
Code:
/[!]^[/!]\w+((-\w+)|(\.\w+)|(\_\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z]{2,4}[!]$[/!]/

On a side note: your profile shows you've asked 37 questions and never given any stars/votes. You've even received 11 in your time here at TT.

I know many people are just unaware of how the system works until it is brought to their attention.

-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
Try this code, it also blocks unwanted characters from the address.

Code:
  var re = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z]{2,4}$/;
  alert(re.test(value));

This requires minimum two characters before and after the @ and requires 2-4 characters after the .


It's hard to think outside the box when I'm trapped in a cubicle.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top