javascriptregexvalidationxregexp

XRegExp for email address


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>


Solution

  • 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: