-2

I've following regular expression: ([A-Za-z0-9.-])\@([a-zA-Z0-9-]{1,63})(.([A-Za-z]{2,6})) and validate test@gamil.com and test@gamil as well. While it must validate test@gmail.com and the email with the sub-domain.

Any help is there to make me correct.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
user2384794
  • 53
  • 1
  • 2
  • 13
  • 3
    I would have a go at an already existing email regex script, like [here](http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address). This is a complicated solution, and writing a custom one may leave some email addresses out that are valid. –  Mar 29 '15 at 04:42
  • 1
    Can you please check your regular expression? It looks to me like you are missing a + before \@. Also, did you mean to escape the .? If you don't, the . will match any character, and that may be your problem. (But I agree with Danbopes' comment: don't roll your own email regular expression.) –  Mar 29 '15 at 04:44

3 Answers3

0

Try this:

  /^([A-Za-z0-9.-])+\@([a-zA-Z0-9-]{1,63})(\.([A-Za-z]{2,6}))$/ 

1) The first ^ and last $ ensure that the whole string is matched

2) You neet a + before the \@ to match the whole name, not just its last character

3) while you do not require a \ before the @ character you DO need one before the .

But as mentioned in the comments you would be better served finding a pre-built one on the web.

HBP
  • 15,685
  • 6
  • 28
  • 34
0
  1. You tried to match only one character before @ sign, but you need to ensure that there is at least one character

    ([A-Za-z0-9.-])+

  2. You missed to escape the . character at the final part of email (.com, .net, .xyz), also add the + modifier to accept both domains and sub-domains

    (.([A-Za-z]{2,6}))+

Finally, it should be something like this

([A-Za-z0-9.-])+\@([a-zA-Z0-9-]{1,63})(\.([A-Za-z]{2,6}))+

See updated regex

Mohamed Shaaban
  • 1,129
  • 6
  • 13
  • I don't want the regular expression return true for test@gmail also I want test@gmail.com and test@uae.gov.pk must also be validated by the regular repression. – user2384794 Mar 31 '15 at 10:40
  • It should now neglects `test@gmail` and accepts `test@gmail.com` and `test@uae.gov.pk` – Mohamed Shaaban Mar 31 '15 at 11:10
0

I done with this. here in the following is the regular expression.

([A-Za-z0-9.-])*\@([a-zA-Z0-9-]{1,63})(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,6})

It validates the following kinda email address:

hello@gmail.com
he_llo@worl.d.com
hel.l-o@wor-ld.museum
h1ello@123.com
hello@world.com.pk

Thank You all for your kinda help.

user2384794
  • 53
  • 1
  • 2
  • 13