0

I am doing email regular expression validation for the email and I am stuck on one point using following expression if user enter something like abc.abc then this expression works fine but when user enters [email protected] then it doesn't work

var myreg = new RegExp(/([a-z0-9.]+)@([a-z0-9]+)\.([a-z0-9]+)/i);
var patter =  myreg.test(document.getElementById("email").value)
alert(patter)
if(patter == false){
  errorMsg.push("email Formate Error Ex:[email protected]");
}

I want that user must enter his email in this formate like [email protected]/.ca/.org

vusan
  • 5,221
  • 4
  • 46
  • 81
user1878049
  • 17
  • 1
  • 1
  • 6

5 Answers5

0
var pattern = /^[a-z0-9]+\.[a-z0-9]+@[a-z0-9]+\.[a-z]+$/i;

pattern.test('[email protected]');
// returns true;

pattern.test('[email protected]');
// returns false;

pattern.test("abc.abc");
// returns false;
xiaoyi
  • 6,641
  • 1
  • 34
  • 51
0

Using Regular Expressions is probably the best way.

Here's an example (live demo):

function validateEmail(email) { 
  var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\ ".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  return re.test(email);
}

But keep in mind that one should not rely only upon JavaScript validation. JavaScript can easily be disabled. This should be validated on the server side as well.

Source

Community
  • 1
  • 1
Charles
  • 1,121
  • 19
  • 30
0

This work Perfectly for me.

/^[a-zA-Z]([a-zA-Z0-9_\-])+([\.][a-zA-Z0-9_]+)*\@((([a-zA-Z0-9\-])+\.){1,2})([a-zA-Z0-9]{2,40})$/;
Anup Panwar
  • 293
  • 4
  • 11
-1

Hope this is helpful in resolving the issue \b[A-Z0-9._%-]+@[A-Z0-9.-]+.[A-Z]{2,4}\b Its case sensitive.

java_dude
  • 4,038
  • 9
  • 36
  • 61
-1

Pattern for to validate email address is:

/^([a-zA-Z0-9_.-])+\@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;

use this pattern in Regex. and you can validate any email..

Naveen Katakam
  • 400
  • 1
  • 8
  • 23