Locked lesson.
About this lesson
How to validate an HTML fill-out form with JavaScript.
Exercise files
Download this lesson’s related exercise files.
Form Validating.docx59.2 KB Form Validating - Solution.docx
59.6 KB
Quick reference
Form Validating
Every fill-out form field can be validated any way we want.
When to use
Whenever someone fills out a form on your website, you can validate anything they enter.
Instructions
Form validation is important, and easy to do.
Name: <input id="fullName">
<button type="button" onclick="myFunction()">Submit</button>
<br/>
<p id="return"></p>
<script>
function myFunction(){
var fullName = document.getElementById("fullName").value;
if (fullName == ""){
document.getElementById("return").innerHTML = "Hey you forgot to enter your name!";
} else {
document.getElementById("return").innerHTML = "Hello " + fullName;
}
}
</script>
Hints & tips
- We can easily validate any form field
- IF/ELSE statements are useful for form validations
- With an IF/ELSE statement, you can validate for whatever you want!
Lesson notes are only available for subscribers.