Source Code:
(back to article)
Submit
Result:
Report an issue
<!DOCTYPE html> <html> <head> <title>Title of the Document</title> <script src="https://code.jquery.com/jquery-3.5.0.min.js"> </script> </head> <body> <form> <p>Enter an email address:</p> <input id='email'> <button type='submit' id='validate'>Validate button</button> </form> <div id='result'></div> <script> function validateEmail(email) { let res = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/; return res.test(email); } function validate() { let result = $("#result"); let email = $("#email").val(); result.text(""); if(validateEmail(email)) { result.text(email + " is valid"); result.css("color", "blue"); } else { result.text(email + " is not valid"); result.css("color", "red"); } return false; } $("#validate").on("click", validate); </script> </body> </html>