0

I tried a regular expression to validate email id. When I use this expression:

var emailxP = /^(\w+([0-9-+.']\w+)*+\@+\w+\.([\\a-z]{2,3})(\.\w+))$/;

the expression is not working the chrome is showing

ncaught SyntaxError: Invalid regular expression: /^(\w+([0-9-+.']\w+)*+\@+\w+\.([\\a-z]{2,3})(\.\w+))$/: Nothing to repeat 

can please tell me what happen in there.

Thank you.

shajib0o
  • 581
  • 2
  • 5
  • 8
  • Try this one as a possible duplication here: http://stackoverflow.com/questions/2855865/jquery-regex-validation-of-e-mail-address – Pavlo Feb 19 '14 at 10:32

2 Answers2

3

The *+ is invalid, you have to choose between + and *

var emailxP = /^(\w+([0-9-+.']\w+)+\@+\w+\.([\\a-z]{2,3})(\.\w+))$/;
//                        here ___^

or

var emailxP = /^(\w+([0-9-+.']\w+)*\@+\w+\.([\\a-z]{2,3})(\.\w+))$/;
//                        here ___^

Also escape the dash

var emailxP = /^(\w+([0-9\-+.']\w+)*\@+\w+\.([\\a-z]{2,3})(\.\w+))$/;
//               here ___^

And why double escape for a?

var emailxP = /^(\w+([0-9\-+.']\w+)*\@+\w+\.([\\a-z]{2,3})(\.\w+))$/;
//                                    here ___^^

I propose this:

var emailxP = /^(\w[\w+.'-]*@\w+(\.[a-z]+)*)$/;
Toto
  • 89,455
  • 62
  • 89
  • 125
0

Try to wrap your regex inside ' ' and also escape the ' inside your regex with \:

var emailxP = '/^(\w+([0-9-+.\']\w+)*+\@+\w+\.([\\a-z]{2,3})(\.\w+))$/';
// ------------------------- ^ here -------------------------------
Felix
  • 37,892
  • 8
  • 43
  • 55