I’m not too fond of javascript, but form a user’s POV it’s often better to process a few tests client-side rather than server-side. Like, I really hate to submit a form, wait 5 seconds on a slow server, then get a message telling me to fill this or that (and occasionnaly lose all other fields data in a failed page refresh ??
Anyway, here is a way to “highlight” a missing mandatory input. I finally did it because it has turned as a challenge for me when I couldnt tell why my first try wasnt working ??
<script language="JavaScript">
<!--
function isValid(thing) {
email=thing.my_field.value;
alert (email);
if (email=="") {
// the form is not ok
// you could also add : alert ('this is a mandatory value');
thing.getElementsByTagName("label")[0].style.color = "red";
alert ('oh');
return false;
} else {
// the form is ok
thing.submit();
return true;
}
}
//-->
</script>
<form name="my_form" id="my_form" method="post" action="yourpage.php">
<label id="my_label" name="my_label" for="my_field">Mandatory value:</label>
<input type="text" id="my_field" />
<input type="button" onClick="return isValid(this.form)" value="Ok" />
</form>