Retrieving Field Names
I want to perform some validation before a user submits a form. Is there any way for me to check each field to see if it's been filled in by the user. Right now, I have to manually add these to my script. I thought maybe I could use javascript to scroll through all fields to see if they'd been filled in or not. Here is my current code below. In my example you see that I have two if statements, one for each field on my form. What if I had 10 or 20 fields on my form. Assuming they are all required fields, rather than keying in 10 or 20 if statements, can I scroll through the fields instead? Thanks
function validateForm(theForm) {
if (!validRequired(theForm.begindate,"Begin Date"))
return false;
if (!validRequired(theForm.enddate,"End Date"))
return false;
return true;
}
function validRequired(formField,fieldLabel) {
var result = true;
if (formField.value == "")
{
alert('Please enter a value for the "' + fieldLabel +'" field.');
formField.focus();
result = false;
}
return result;
}
|