0

I found this Regex in stack overflow for the email validation but this regex won't validate the invalid characters in the email. I couldn't find a proper answer to it.

If the email is Vinu'a@gmail.com this gives as a valid email address.

var patternForEmailValidation = /^(([^<>()[\]\\.,;:\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,}))$/;
var patternForEmailValidation = patternForEmailValidation.test(email);
Buvin Perera
  • 481
  • 1
  • 6
  • 18
  • you want only alpha and digit?? – Hackaholic Nov 04 '14 at 05:51
  • yes, all the invalid characters should be validated in a email addy . !#$%&*() – Buvin Perera Nov 04 '14 at 05:52
  • dose those domains contain these characters ? ( '"!#$%&^*) – Buvin Perera Nov 04 '14 at 05:54
  • Probably not (in international domains), but they certainly are not 'ascii' characters. Also: `Vinu*a@gmail.com` **is** a perfectly valid mail-address.. might not be to google, but since when do they make the rules `:)` There won't be a lot of people burning their fingers on this futile exercise. – GitaarLAB Nov 04 '14 at 05:57
  • but shouldn't we have to consider ' ? above regex won't validate that also :( – Buvin Perera Nov 04 '14 at 06:00
  • Nope we shouldn't. We should accept whatever the user feeds us and send the confirmation mail already! – GitaarLAB Nov 04 '14 at 06:01
  • if I have something like vinu'@gmail.com , it will break in the server side is it not possible to handle those ? – Buvin Perera Nov 04 '14 at 06:04
  • 1
    Then.. like any input, you'd escape it, otherwise messing up your database would be loads-o-fun to some! (Pretty much the only thing to check on is the existence of an @ sign and at least one dot. All the rest is futile (well, it was before but now (international urls and tld-domains) it really is). – GitaarLAB Nov 04 '14 at 06:06
  • 1
    @GitaarLAB: I don't think there is a need to escape the input if the db application is done properly with prepared statement. – nhahtdh Nov 04 '14 at 06:09

1 Answers1

-1

Try following regex

var em_regex =  /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
Domain
  • 11,562
  • 3
  • 23
  • 44
  • I have used this expression at many places, but I did not find any issues till now. @GitaarLAB, can you give me any example where it can be failed, so that I can make corrections. – Domain Nov 04 '14 at 06:17
  • @GitaarLAB, I just tried the example you have specified. But in my case it validated the email and shown me the error. – Domain Nov 04 '14 at 06:25
  • Ok, I see I missed the `\.` hei@やる.ca would indeed work, my bad for that, sorry. But it does fail for `email@-example.com`, or `Abc..123@example.com`, `email@[123.123.123.123]`, granted tough, it is better than I thought at first, I'll remove my first comment, that's to harsh. – GitaarLAB Nov 04 '14 at 06:34
  • Have a look here: http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html (mind you, also not perfect..), Oh, and here: http://programmers.stackexchange.com/questions/78353/how-far-should-one-take-e-mail-address-validation – GitaarLAB Nov 04 '14 at 06:39
  • Icing on the cake: http://stackoverflow.com/a/201378/588079 and other answers there. – GitaarLAB Nov 04 '14 at 07:03