Prev Demo
JavaScript
>
Validating HTML Form
Next Demo
Write code here
J
|
B
|
A
<!DOCTYPE html> <html> <head> <title>Demo</title> </head> <body> <h1>Validating HTML form elements</h1> <script> function ValidateTheForm() { if (document.getElementById("txtFirstName").value == '') { alert("Please specify first name, it is mandatory !"); return false; } else { return true; } } </script> <form action="HTMLPage.htm" method="post"> First Name: <input type="text" id="txtFirstName" name="txtName" /> * Last Name: <input type="text" id="txtLastName" name="txtName" /> <input type="submit" id="btnSubmit" value="Submit" onclick="return ValidateTheForm()" /> </form> <br/> <p><strong>Note:</strong> Onclick of "Submit" button with the empty text boxes gives the alert.</p> </body> </html>
Note: We DO NOT save your trial code in our database.
Output