Click to See Complete Forum and Search --> : Multiple field on multiple line validation


andytate
06-03-2005, 06:34 AM
I'm trying to validate a form which has twelve lines of information that the user can enter information on. Line1 has a fields qty1, desc1, stk1, glcol1, currency1, prc1 and slcUnit1. The other 11 lines are exact copies of Line1 with the numbers incresing accordingly. eg. qty2, desc2 etc. etc.

function validate_form ( )
{
valid = true;

if ( document.newReq.Qty1.value != "" && document.newReq.Desc1.value == "" || document.newReq.Stk1.value == "" || document.newReq.GLcolumn1.value =="" || document.newReq.currency1.selectedIndex == 0 || document.newReq.Prc1.value =="" || document.newReq.slcUnit1.selectedIndex == 0)
{
alert ( "Please check all entries for line 1 are completed." );
valid = false;
}
return valid;
}

I want to create a loop that will repeat the above for the 12 lines incrementing the number sequence automatically rather than write this statement 12 times and change the numbers myself.

I hope i've explained myself and I know this is probably easy to most of you guys but I'm new to Javascript, forgive me

AdamBrill
06-03-2005, 07:19 AM
Well, it should look something like this:function validate_form ( )
{
valid = true;
for(var x=0; x<12; x++){
if ( document.newReq.elements['Qty'+x].value != "" && document.newReq.elements['Desc'+x].value == "" || document.newReq.elements['Stk'+x].value == "" || document.newReq.elements['GLcolumn'+x].value =="" || document.newReq.elements['currency'+x].selectedIndex == 0 || document.newReq.elements['Prc'+x].value =="" || document.newReq.elements['slcUnit'+x].selectedIndex == 0)
{
alert ( "Please check all entries for line 1 are completed." );
valid = false;
}
return valid;
}This is untested, so sorry if I made a typo or something. You should be able to get the just of it either way.

Also, you may want to do some form validating server-side, since anyone can turn off JS and break the best JS form validating that exists.

andytate
06-03-2005, 07:29 AM
Also, you may want to do some form validating server-side, since anyone can turn off JS and break the best JS form validating that exists.

Thanks adam I'll give this a go, it looks sensible enough to a beginner like myself, I should be able to get it working. This is an internal application on our intranet so I know all machines in our network will have JS turned on in their browsers.

andytate
06-07-2005, 09:36 AM
OK I've tried the code offered by Adam, I've modified it to be as follows...

for(var x=1; x<=12; x++){
if ( document.newReq.elements['Qty'+x].value != "" && document.newReq.elements['Desc'+x].value == "" || document.newReq.elements['Stk'+x].value == "" || document.newReq.elements['GLcolumn'+x].value =="" || document.newReq.elements['currency'+x].selectedIndex == 0 || document.newReq.elements['Prc'+x].value =="" || document.newReq.elements['slcUnit'+x].selectedIndex == 0)
{
alert ( "Please check all entries for line 1 are completed." );
valid = false;
}
}
return valid;


This works partially, however if you leave a field empty on line (x) then the script is supposed to notify you that line (x) has a field that needs addressed. It currently loops through the script 12 times.

I should also say that if there is no entry in field Qty(x) then the form should recognise the fact that all other fields on that line are empty and this is ok.

I hope I've explained this correctly.