2

I want to write regular expression for email address for all including non-italic characters.

I tried but it return false

Please provide correct solution as soon as possible

 <!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/xregexp/3.1.1/xregexp-all.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script type="text/javascript">
 var em = XRegExp('^([\\p{L}+|\\p{N}*][@][\\p{L}+][.][\\p{L}+])$'); // Please help me to correct it
 jQuery(function(){
  jQuery('input').blur(function(){
   console.log(jQuery(this).val());
   console.log(em.test(jQuery('#t1').val()));
   
  });
 });
 
</script>
 <title></title>
</head>
<body>
Enter Name: <input type="text" name="t1" id="t1" class="kcd">
</body>
</html>
Sparky
  • 94,381
  • 25
  • 183
  • 265
kailash Chandra
  • 726
  • 5
  • 12

1 Answers1

1

While there are better means to make sure your email regex is valid (see @Tushar's comment), I'd like to explain what the problem is with your regex.

The ^([\\p{L}+|\\p{N}*][@][\\p{L}+][.][\\p{L}+])$ contains incorrectly formed character classes [\\p{L}+|\\p{N}*] and [\\p{L}+]. They match a single character defined inside them - [\\p{L}+|\\p{N}*] matches either a p, {, L, etc. and [\\p{L}+] matches either a p, {, L, }, or +.

If you plan to use your approach, you might want to fix the regex as

XRegExp('^[\\p{L}\\p{N}]+@\\p{L}+[.]\\p{L}+$')

Details:

  • ^ - start of string
  • [\\p{L}\\p{N}]+ - one or more Unicode letters or digits
  • @ - "at" sign
  • \\p{L}+ - one or more Unicode letters
  • [.] - a literal dot
  • \\p{L}+ - ibid.
  • $ - end of string.
Community
  • 1
  • 1
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397