...

View Full Version : Form Validation and Submittal Error



martyf
09-24-2005, 10:43 AM
I have a form on a page that is built at runtime which enables the user to specify an image to upload and an ALT attribute. The file fields are in the format "fileupload_1", "fileupload_2", etc, and the ALT attributes are similar, but with 'alt' instead of 'fileupload'.

I added this line to the form:

onsubmit="return validator(this)"

And then

function validator(thisform)
{
with (thisform)
{
for (var i = 0; i < elements.length; i++) {
if (elements[i].name.substring(0, elements[i].name.lastIndexOf("_")) == "fileupload"){
if (notEmpty(elements[i],"Image "+elements[i].name.substring(elements[i].name.lastIndexOf("_")+1)+" requires a file to upload")==false) {
elements[i].focus();
return false;
}
}
if (elements[i].name.substring(0, elements[i].name.lastIndexOf("_")) == "alt"){
if (notEmpty(elements[i],"Image "+elements[i].name.substring(elements[i].name.lastIndexOf("_")+1)+" requires an ALT text")==false) {
elements[i].focus();
return false;
}
}
}
}
}

The notEmpty function just returns true or false if the value of the field is empty or not.

The script works if it encounters an error, but I can't get it to submit the form when it doesn't find an error. I have tried adding a 'return true;' statement, as well as forcing the form to submit after the 'for' loop but haven't had any luck. Does anyone have any suggestions?

Cheers,
Marty

martyf
09-24-2005, 11:26 AM
ooh solved my own problem - it works only if the file field is a real path.

Cheers,
Marty



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum