JavaScript Form Validation
JavaScript Form Validation
If a form field (fname) is empty, this function alerts a message, and returns
false, to prevent the form from being submitted:
JavaScript Example
function validateForm() {
let x = document.forms["myForm"]["fname"].value;
if (x == "") {
alert("Name must be filled out");
return false;
}
}
Try it Yourself »
Submit
Try it Yourself »
ADVERTISEMENT
Automatic HTML Form Validation
HTML form validation can be performed automatically by the browser:
Try it Yourself »
Automatic HTML form validation does not work in Internet Explorer 9 or
earlier.
Data Validation
Data validation is the process of ensuring that user input is clean, correct,
and useful.
Most often, the purpose of data validation is to ensure correct user input.
Server side validation is performed by a web server, after input has been
sent to the server.