Click to See Complete Forum and Search --> : loop help!


mast3r
08-27-2003, 04:04 AM
i have the following javascript and at the moment it only returns 1 field missing ata a time. Can some1 either create me a loop for this so it checks all fileds and gives one output of all missing fields. Or can some1 change my javascript to do that without using a loop either is ok thanks.

function checkForm(form){


if(form.first_name.value==""){
window.alert('Question 1 Missing');
return false;
}
else if(form.last_names.value==""){
window.alert('Question 2 Missing');
return false;
}
else if(form.age.value==""){
window.alert('Question 3 Missing');
return false;
}
else if(form.address_line1.value==""){
window.alert('Question 4 Missing');
return false;
}
else if(form.address_line2.value==""){
window.alert('Question 5 Missing');
return false;
}
else if(form.phone_work==""){
window.alert('Question 6 Missing');
return false;
}
else if(form.filters.value==""){
window.alert('Question 7 Missing');
return false;
}
else if(form.customer_contact.value==""){
window.alert('Question 8 Missing');
return false;
}
else if(form.customer_tel.value==""){
window.alert('Question 9 Missing');
return false;
}
else if(form.equipment.value==""){
window.alert('Question 10 Missing');
return false;
}
else if(form.youremail.value==""){
window.alert('Question 12 Missing');
return false;
}
}

Gollum
08-27-2003, 04:53 AM
How 'bout

function checkForm(form)
{
var aChk =
[
[form.first_name, "Question 1"],
[form.last_names, "Question 2"],
[form.age, "Question 3"],
[form.address_line1, "Question 4"],
[form.address_line2, "Question 5"],
[form.phone_work, "Question 6"],
[form.filters, "Question 7"],
[form.customer_contact, "Question 8"],
[form.customer_tel, "Question 9"],
[form.equipment, "Question 10"],
[form.youremail, "Question 12"]
];

var aMsg = new Array;
for ( var i = 0; i < aChk.length; i++ )
{
if ( aChk[i][0].value == "" ) aMsg.push(aChk[i][1]);
}

if ( aMsg.length > 0 )
{
alert(aMsg.join(', ') + "Missing");
return false;
}

return true;
}

pyro
08-27-2003, 07:55 AM
Take a look at http://www.infinitypages.com/research/formvalidation.htm or http://www.infinitypages.com/research/formvalidation_2.htm

mast3r
08-27-2003, 09:04 AM
Thanks guys im sorted now!