This article will show you how you can validate an email address by regex using jQuery in your web application.
So first we will check the Regular expression to validate out email id.
So first we will check the Regular expression to validate out email id.
"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$" |
Now let’s check the code. Here we will create a function which will validate the value as per passed regular expression.
function ValidateRegeex(regex, value) { var expre = new RegExp(regex); return expre.test(value); } |
Now let’s use our validation function to validate the email id.
<input type="text" id="txtEmailId" /> <input type="button" value="Submit" onclick="javascript: ValudateEmaiId();"/> <script> function ValudateEmaiId() { var regex = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/; var value = $("#txtEmailId").val(); var status = ValidateRegeex(regex, value); if (status == true) { alert("correct Email id"); } else { alert("Enter a Valid email id"); } } </script> |
Now check the output. As u enter valid or not a valid email id. you will get an error message.
As you enter correct email id you will get valid message.
0 comments:
Please let me know your view