Click to See Complete Forum and Search --> : js 'for' loop to validate multiple fields


bloke
09-17-2003, 06:07 AM
I have a form that passes in 18 sets of 6 fields (Item1 - Item18, Quantity1 - Quantity18 and so on..).

I have a javascript function which validates various fields of the same form and that works fine however I also need to validate the fields mentioned above, eg if Item1 has a value then Quantity1 must contain a value.

Rather than writing a bit of code for each field, I was hoping to be able to loop through thus:



for(i=1; i<19; i++) {
if ((document.frm.Item[i].value != 0) && (document.frm.Quantity[i].value == 0))
{
alert("Please enter a quantity")
document.frm.Quantity[i].focus()
return false
}
}



..but this doesn't seem to work. I don't get an error and the code above it still works. It just seems to ignore everything from this point on.

Can anyone tell me what I'm doing wrong?

Cheers

gil davis
09-17-2003, 07:39 AM
Form field values are strings, not numbers. If the string value does not equate to a numeric zero, then you pass the validation with no errors.

Either check the length of the field or check the field for the empty string ("").

bloke
09-17-2003, 07:46 AM
Hmm, but I've used this bit of code for years and it works fine.


if (document.frm.Item1.value == 0)
{
alert("Please enter at least one item for this order")
document.frm.Item1.focus()
return false
}



If I take the test out of the loop and replace the variables with actual form field names, it works too:


if ((document.frm.Item1.value != 0) && (document.frm.Quantity1.value == 0))
{
alert("Please enter a quantity")
document.frm.Quantity1.focus()
return false
}



Looking at it a bit more, I don't think the loop is the problem, I think the problem is that it doesn't equate:

document.frm.Item[i]

to be

document.frm.Item1, document.frm.Item2 (etc)

If that makes any sense?!


:confused:

dragle
09-17-2003, 09:44 AM
Hiya!

Looking at it a bit more, I don't think the loop is the problem, I think the problem is that it doesn't equate:

document.frm.Item[i]

to be ...

Right. JavaScript will only convert the form elements to an array if they are all named the same thing:

<form name="theForm" ...
<input type="text" name="Quantity" ...
<input type="text" name="Quantity" ...
...
var q1 = document.theForm.Quantity[0].value;
var q2 = document.theForm.Quantity[1].value;

And note that in that case, the first entry is Quantity[0], not Quantity[1]. To keep your current naming structure, you would need to do something like:

if ((document.frm.elements["Item"+i].value != 0) &&
(document.frm.elements["Quantity"+i].value == 0)) {

Also, Gil's point is well taken; I'm assuming that elsewhere you are ensuring the values you have are numeric in nature (since you're comparing only to zero) otherwise a person could put "foo" in the item field and "bar" in the quantity field and your validation above would still "pass" ...

Cheers!

bloke
09-17-2003, 09:48 AM
Hi, thanks for your help. I seem to have almost cracked it using:


for(i=1; i<19; i++) {
var thing = "Item"+i
var something = "Quantity"+i

var otherthing = eval("document.frm."+thing+".value")
var somethingelse = eval("document.frm."+something+".value")

if ((otherthing != 0) && (somethingelse == 0))
{
alert("Please enter a quantity")
document.frm.somethingelse.focus()
return false
}
}


but I need to exit the loop at return false. Is this possible? I've tried adding 'break' after the return false but it doesn't seem to do the trick.

ps, I have an additional check on the fields being numeric using onBlur to call:



function checkNaN(obj){
if (isNaN(obj.value))
{
alert("Please enter only numeric characters")
obj.focus()
return false
}
}


Cheers:(

dragle
09-17-2003, 10:25 AM
The return statement itself should exit the loop; as well as the enclosing function.

This line isn't working as you intend:

document.frm.somethingelse.focus()

Since (presumably) you don't have a field in your form called "somethingelse." Try this instead:

document.frm.elements[somethingelse].focus()

Cheers!

bloke
09-17-2003, 10:31 AM
Nah mate, tried it, same thing happens (ie the script checks that there are no more fields to validate and submits the form). It continues the loop through all 18 possibilities and as the last one is not false then it assumes all is well and submits. Kind of thing.

It does actually focus on the 'somethingelse' field (which is set earlier as 'Quantity+i' ( I have fields in the form ranging from Quantity1 through to Quantity18) but it then loops through the rest.

I know I've probably just repeated myself but it's been a long day and my head hurts! :D

Anyhoo, I've just commented out the 'focus' line to get by and it works.

Thanks for your help. You've helped to clear the fog!

Cheers

akshay01
05-23-2008, 07:07 AM
Hi,
I saw your post in which you were facing some problem regarding
"js 'for' loop to validate multiple fields".
I am also facing the same issue, could you please guide me how to resolve it??
my code is :

function check()

{

no_of_row = document.form1.dev.value

// alert(no_of_row);

for(l=0;l<no_of_row;l++)

{

var txt="Text"+l; alert(txt);
var chk="Chk"+l; alert(chk);}

if(document.form1.chk.checked ==true)
{

var v=document.form1.txt.value;
if(v!="")
alert(v);
else
alert("Please enter id");

return true;

}

else

{



alert('Please check the assign token');
return false;
}



}

}

In this code i have to check the value of multiple checkboxes, and if value is checked then i have to store the value of text box and the pass that value into another page .
could you please help me ?
thanks in advance !!!

dragle
05-23-2008, 08:35 AM
I'm making a lot of assumptions about your form here, but...

If your checkboxes and form fields all have unique IDs, you could just:
if (document.getElementById(chk).checked) {
var v=document.getElementById(txt).value;
Otherwise, you would need to do something like:
if (document.form1.elements[chk].checked) {
var v=document.form1.elements[txt].value;
Good luck!