jQuery provides RegExp class, and RegExp has test method which check your pattern. You have to pass the pattern in test method. It will check and return true if matches the email string according to your pattern.
In this given example firstly I check the value of textbox if it is empty I have display an alert message otherwise I have pass the textbox value in "isValidEmail" method, if this method return false i have display "Invalid Email" alert message.
CODE
<!DOCTYPE html> <html> <head> <title>jQuery With Example</title> <script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $('.btnLogin').click(function (event) { var email = $('.txtEmail').val(); if ($.trim(email) == '') { alert("Please Enter Email"); } else { if (isValidEmail(email) == false) { alert("Invalid Email"); } else { alert("Good"); } } }); }); function isValidEmail(email) { var pattern = new RegExp(/^(("[\w-+\s]+")|([\w-+]+(?:\.[\w-+]+)*)|("[\w-+\s]+")([\w-+]+(?:\.[\w-+]+)*))(@((?:[\w-+]+\.)*\w[\w-+]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i); return pattern.test(email); } </script> </head> <body> <div> Email: <input type="text" class="txtEmail" /> <br /><br /> <input type="button" class="btnLogin" value="Login" /> </div> </body> </html>
DISPLAY
Email:
No comments :
Please Give Your Feedback