Form validation ensures that users input the correct data in forms before submitting them.
We can use JavaScript to check if a form field is empty:
function validateForm() {
let name = document.getElementById("name").value;
if (name === "") {
alert("Name must be filled out");
return false;
}
}
HTML5 provides attributes like required
, minlength
, and maxlength
for simple validation.
Create a form with fields for name
and email
. Add basic validation to check if these fields are filled.
required
attribute in HTML5?Answers: Ensures correct input; return false in JavaScript; makes a field mandatory.