1

I have wrote the below function to validate the email address entered by user.

function checkEmail(v_email)
{
var l_ret=true;
var l_reg = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/;
if (l_reg.exec(v_email)==null){l_ret=false;}
return l_ret;
} 

It works perfectly fine.. with lowercase email address, (example.myemail@example.com) but if it detect any capital letter then it fails. like (example.MYemail@example.com).

I am trying to make it work for both capital as well as lower letter but i am not able to make it.. any one good with regex..could please suggest.

thank you in advance...

regards, Mona..

SmartDev
  • 481
  • 3
  • 11
  • 30
  • Of course this regular expression only matches a subset of valid email addresses, and including capitals will not improve the situation much. For something better see http://www.regular-expressions.info/email.html – Jon Oct 05 '13 at 10:56
  • [Stop Validating Email Addresses With Complicated Regular Expressions](http://davidcel.is/blog/2012/09/06/stop-validating-email-addresses-with-regex/) – Anirudha Oct 05 '13 at 11:08

4 Answers4

1

Simply use the case insensitive mode:

/^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/i;
                                            ^

It makes [a-z] also match uppercase letters. Otherwise, you can use [A-Za-z] to mean both upper and lowercase characters in regex.

Jerry
  • 70,495
  • 13
  • 100
  • 144
  • Hi Jerry.. thank you for reply.. just would like to know the above my regex will validate whole email address or just part..as JON said above...so thought will ask you.. – SmartDev Oct 05 '13 at 10:59
  • even space is a valid character in emailid..so are ;:^!()%...Don't use regex to parse email..plz – Anirudha Oct 05 '13 at 11:03
  • @monamona Yes, I know. I simply answered your question; which was to make the regex work for both uppercase and lowercase letters. Parsing valid email addresses is another question and the site has a lot of duplicate questions like that. – Jerry Oct 05 '13 at 11:06
  • @monamona:- You may find this Thread helpful:- http://stackoverflow.com/questions/46155/validate-email-address-in-javascript – Rahul Tripathi Oct 05 '13 at 11:08
  • 1
    Thank you all for your valuable answers... at this moment this solved my purpose.. so i will go ahead with this. thank you once again.. – SmartDev Oct 05 '13 at 11:29
1

Noooooooooooo you DONT use regex to validate EMAIL..

An Email is valid if you can send a mail to it..

To validate Email,follow these steps..

  • 1>Send Mail to that email address in which you can put an activation code or even a link.

  • 2>If you receive the respose,email is valid..


At max your regex should be

^[^@]+@[^@]+$

Stop Validating Email Addresses With Complicated Regular Expressions

Anirudha
  • 32,393
  • 7
  • 68
  • 89
0

Change your regex like this:

var l_reg = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/i;

instead of

 var l_reg = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/;
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • even space is a valid character in emailid..so are ;:^!()%...Don't use regex to parse email..plz – Anirudha Oct 05 '13 at 11:02
  • @Anirudh:- Very correct! But OP's question is very specific: **I am trying to make it work for both capital as well as lower letter but i am not able to make it** – Rahul Tripathi Oct 05 '13 at 11:04
0

Make the regex ignore the case by adding the flag i like below:

var l_reg = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/i;
Harry
  • 87,580
  • 25
  • 202
  • 214
  • even space is a valid character in emailid..so are ;:^!()%...Don't use regex to parse email..plz – Anirudha Oct 05 '13 at 11:03
  • @Anirudh: I am not exactly recommending this method. I am merely answering the OP's question on how to make it ignore case. – Harry Oct 05 '13 at 11:05