jQuery <form> handle submit event
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Executing a Function on Form Submit Event in jQuery</title> <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> <style> .error{//from ww w.j a v a 2 s . c o m color: red; } </style> <script> $(document).ready(function(){ $("form").submit(function(event){ var regex = /^[a-zA-Z]+$/; var currentValue = $("#firstName").val(); if(regex.test(currentValue) == false){ $("#result").html('<p class="error">Not valid!</p>').show().fadeOut(1000); // Preventing form submission event.preventDefault(); } }); }); </script> </head> <body> <p>If try to submit any invalid value. It will produce an error.</p> <form action="action.php" method="post" id="users"> <label for="firstName">First Name:</label> <input type="text" name="first-name" id="firstName"> <input type="submit" value="Submit"> <div id="result"></div> </form> </body> </html>