http://www.interspir...Form-Validation
Right now I am interested in using inline form validation for my school project. The above link has the code, however only ONE red error message appears at a time in order from the top. Is there a way to code it such that all those empty fields will have an error message appearing beneath it? instead of only one field at a time?
<html>
<style>
input {
width: 200px;
font-family: Tahoma;
font-size: 8pt;
}
.label {
width:50px;
}
textarea {
width: 200px;
font-family: Tahoma;
font-size: 8pt;
}
body {
font-family: Tahoma;
font-size: 8pt;
}
.error {
font-family: Tahoma;
font-size: 8pt;
color: red;
margin-left: 50px;
display:none;
}
</style>
<script>
function checkForm() {
name = document.getElementById("name").value;
email = document.getElementById("email").value;
comment = document.getElementById("comment").value;
if (name == "") {
hideAllErrors();
document.getElementById("nameError").style.display = "inline";
document.getElementById("name").select();
document.getElementById("name").focus();
return false;
} else if (email == "") {
hideAllErrors();
document.getElementById("emailError").style.display = "inline";
document.getElementById("email").select();
document.getElementById("email").focus();
return false;
} else if (comment == "") {
hideAllErrors();
document.getElementById("commentError").style.display = "inline";
document.getElementById("comment").select();
document.getElementById("comment").focus();
return false;
}
return true;
}
function hideAllErrors() {
document.getElementById("nameError").style.display = "none"
document.getElementById("emailError").style.display = "none"
document.getElementById("commentError").style.display = "none"
}
</script>
<body>
<form onsubmit="return checkForm();" method=post action="myscript.php">
<span class=label>Name:</span><input type=text value="" id=name><br>
<div class=error id=nameError>Required: Please enter your name<br></div><br>
<span class=label>Email:</span><input type=text value="" id=email><br>
<div class=error id=emailError>Required: Please enter your email address<br></div><br>
<span class=label>Comment:</span><textarea type=text value="" id=comment></textarea><br>
<div class=error id=commentError>Required: Please enter a comment<br></div><br>
<input type=submit value=Submit style="margin-left: 50px">
</form>
</body>
</html>

New Topic/Question
Reply



MultiQuote





|