A versatile solution is to use a combination of scripts to accomplish this. In your form tag, call a script to validate the form. This script checks the field, totals up all fields that don't validate, and displays an alert listing those fields. In this example, it calls a checkEmpty script, but there are also short scripts for checkURL, checkNumber, checkDate, etc. that can validate fields for the particular format you want. In your form tag, call the fucntion like this:
<form name="myForm" action="results.html" method="post" onSubmit="return validateForm(this);">
Code:
function validateForm(frm) {
checkEmpty(frm.date_ent, "Date Entered")
if (strBlanks != "" || strErrors != "")
{
strErrors = formErrorSentence(strBlanks, strErrors);
alert(strErrors);
strErrors = "";
strBlanks = "";
return false;
}
else
{
return true;
}
}
function checkEmpty(objField, strFieldDescription)
{
if (objField.value == "")
{
strBlanks += "\n" + strFieldDescription;
return false;
}
else
{
return true;
}
}
function formErrorSentence(strEmpty, strInvalid)
{
msg = "_____________________________________________________________________\n\n";
msg += "The request could not be carried out due to the following error(s).\n";
msg += "Please correct these error(s) and re-submit.\n";
msg += "_____________________________________________________________________\n\n";
msg += strInvalid;
if (strEmpty != "")
msg += "\n- The following required field(s) are empty:" + strEmpty;
return msg;
}
I hope this isn't confusing, it's really quite simple once you see what's going on. This may be overkill for what you're trying to do, but when you start to get into long forms, this is an efficient and easy way to validate.
Bookmarks