Click to See Complete Forum and Search --> : Help with from validation script


JackTheTripper
04-04-2003, 03:13 PM
Seems to be a common problem.

Couldn't find one I liked so here is my attempt but I keep getting this error... document.shopper.elements[...].value is null or not an object. :(

Please help




<Script>
var formOK

function checkField(field,pic) {
if (document.shopper.elements[field].value == '') {

document.images[pic].src = "images/error.gif";
formOK = false
}

function formCheck(theForm) {

// fields to validate
var fieldRequired = Array("PreApproved","Have you test-driven the car","New or Used Car","Make", "Model", "Year");
var fieldPic = Array("preapproved","testdrive","newUsed","make", "model", "year");


for (var i = 0; i < fieldRequired.length; i++){
checkField(fieldRequired[i],fieldPic[i])
}

if (formOK == false) {
alert ('Missing Info');
return false;
} else {
return true;
}
}

</script>

Nedals
04-04-2003, 04:06 PM
var formOK

function checkField(field,pic) {
if (document.shopper.elements[field].value == '') {
document.images[pic].src = "images/error.gif";
formOK = false
}
} // missing

function formCheck(theForm) {
// fields to validate
var fieldRequired = new Array("PreApproved","Have you test-driven the car","New or Used Car","Make", "Model", "Year");
var fieldPic = new Array("preapproved","testdrive","newUsed","make", "model", "year");
for (var i = 0; i < fieldRequired.length; i++) {
checkField(fieldRequired[i],fieldPic[i])
}
if (formOK == false) { alert ('Missing Info'); return false; }
else { return true; }

// You could do this instead, loosing the checkField function
//**********
for (var i = 0; i < fieldRequired.length; i++) {
if (document.shopper.elements[fieldRequired[i]].value == '') {
document.images[fieldPic[i]].src = "images/error.gif";
alert ('Missing Info'); return false;
} else { return true }
}
//********
}

khalidali63
04-04-2003, 04:10 PM
Make sure field does represent a form field.(either field name or index)

Cheers

Khalid

JackTheTripper
04-04-2003, 06:31 PM
Beenworking on this all day and can't figure it out. How can you reference a series of radio buttons using an array. The stupid angle brackets keep getting in the way.

// the array
var fieldRequired = new Array("Pre Approoved","Have you test-driven the car","New or Used Car","Do you have a trade in");

// trying to run through the array using an if statement

for (var i = 0; i < fieldRequired.length; i++) {
if (!document.shopper.elements[fieldRequired[i].checked) {
alert('hellow world')
}
}

Without the array it would be document.shopper.radio_name[0 or 1 etc...].checked, right? But I don't know how to writhe this out because of the [] to reference the array

:mad:

Sorry for the mad face. Thanks in advance. :D

Nedals
04-04-2003, 06:43 PM
function validate() {
for (var i = 0; i < fieldRequired.length; i++) {
var field = eval("document.shopper['"+fieldRequired[i]+"']");
if (!field.checked) { alert('hellow world') }
}
return false;
}

Based on your array text, this assumes you meant checkboxes not radio button. I could be wrong?