Regex for mail

13.Jul.2010

In order to validate mail address, you can use regex or some other validation format, but regex is more common. So, here is regex for mail validation:

/^([w!#$%&'*+-/=?^`{|}~]+.)*[w!#$%&'*+-/=?^`{|}~][email protected]((((([a-z0-9]{1}[a-z0-9-]{0,62}[a-z0-9]{1})|[a-z]).)+[a-z]{2,6})|(d{1,3}.){3}d{1,3}(:d{1,5})?)$/i

Next snippets will show you how to use regex for mail in PHP and JavaScript in order to validate users e-mail.

Regex for mail in PHP:

1
2
3
4
5
function isValid($mail) {
	$regex = '/^([w!#$%&'*+-/=?^`{|}~]+.)*[w!#$%&'*+-/=?^`{|}~][email protected]((((([a-z0-9]{1}[a-z0-9-]{0,62}[a-z0-9]{1})|[a-z]).)+[a-z]{2,6})|(d{1,3}.){3}d{1,3}(:d{1,5})?)$/i';
	if(eregi($regex, $mail)) return true
	else return false;
}

Regex for JavaScript:

1
2
3
4
function isValid(mail) {
	var regex = /^([w!#$%&'*+-/=?^`{|}~]+.)*[w!#$%&'*+-/=?^`{|}~][email protected]((((([a-z0-9]{1}[a-z0-9-]{0,62}[a-z0-9]{1})|[a-z]).)+[a-z]{2,6})|(d{1,3}.){3}d{1,3}(:d{1,5})?)$/i;
	return regex.test(mail);
}