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 advxyz@pqr.com 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:firstname.lastname@abc.com");
}

I want that user must enter his email in this formate like firstname.lastname@abc.com/.ca/.org

vusan
  • 5,221
  • 4
  • 46
  • 81
user1878049
  • 17
  • 1
  • 1
  • 6
  • what do you mean *works* and *doesn't work*, your RE looks OK, except there is no need to use `new RegExp()` wrap a `RegExp` instance. – xiaoyi Dec 07 '12 at 03:23
  • 1
    I'd suggest this very forgiving regex: `/[^@]+@[^@]/`, otherwise you can't know for sure if you're leaving some valid email out or you end up with a million character regex. The _only_ way to know if an email is really valid is to send an email and get a response back. – elclanrs Dec 07 '12 at 03:24
  • `myreg.test("firstname.lastname@abc.com");` returns `true` – Michael Berkowski Dec 07 '12 at 03:25
  • @MichaelBerkowski I guess that's how it doesn't work... – xiaoyi Dec 07 '12 at 03:27
  • @xiaoyi Yeah I don't get this question. All the other examples given return true too. – Michael Berkowski Dec 07 '12 at 03:28
  • my email id checking criteria is like firstname.lastname@abc.com if someone enter like jsmith@abc.com then it is error. there should be a "." in between firstname and lastnname – user1878049 Dec 07 '12 at 03:33

5 Answers5

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

pattern.test('firstname.lastname@abc.com');
// returns true;

pattern.test('jsmith@abc.com');
// 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