Click to See Complete Forum and Search --> : Checking input boxes in a for loop


james_cwy
12-07-2003, 08:39 PM
I know that say if we have 3 input boxes.
<input type="text".... name"name1">
<input type="text".... name"name2">
<input type="text".... name"name3">

To check the fields, it is easy as we can just hardcode it.
if document.form.name1.value == " ", etc...

But if I have something like this to generate number of input boxes based on user input.
User enters 3 to generate number of input boxes.Pass this 3 to the next form, then use a for loop to do the input box rather than typing one by one:
$num=$_POST['number'];//that is 3
for ($i=1;$i<=$num;$i++)
{
<td align=center><input type="text" name="name<?php echo $i;?>"></td>
}
The end result will also be:
<input type="text".... name"name1">
<input type="text".... name"name2">
<input type="text".... name"name3">

My question here: How can we check the input boxes when they are generated using for loop?
I hope you get what I mean. I am new to this and really do not know how to go about it.
Hope someone can provide some guidance.

ray326
12-07-2003, 10:12 PM
One way -- you could write the PHP such that it generated the javascript you need along with the inputs since all the information is known on the server at that point.

Another -- you could just PHP generate a length variable for use in a generic looping function.

<script>
var incount=<?php echo $i;?>
...
function checkem()
{
for (i = 1; i < incount; i++)
dostuff(document.formname["name"+i])
}
...
</script>

james_cwy
12-10-2003, 09:29 PM
I am really new at this, so I really need your help. Say I want to use the code (got from the internet) below to check the input boxes generated by the loop.
<!-- Begin
var mikExp = /[$\\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\`\~\=\|]/;
function dodacheck(val) {
var strPass = val.value;
var strLength = strPass.length;
var lchar = val.value.charAt((strLength) - 1);
if(lchar.search(mikExp) != -1) {
var tst = val.value.substring(0, (strLength) - 1);
val.value = tst;
}
}
function doanothercheck(form) {
if(form.value.length < 1) {
alert("Please enter something.");
return false;
}
if(form.value.search(mikExp) == -1) {
alert("Correct Input");
return false;
}
else {
alert("Sorry, but the following characters\n\r\n\r@ $ % ^ & * # ( ) [ ] \\ { + } ` ~ = | \n\r\n\rare not allowed!\n");
form.select();
form.focus();
return false;
}
alert("Correct Input");
return false;
}
// End -->
</script>

</HEAD>
<BODY>

<form name=xyz onSubmit="return doanothercheck(this.txtTerm);">
<input type="text" name="txtTerm" size="35" maxlength="50" value="" onKeyUp="javascript:dodacheck(xyz.txtTerm);">
<br>
<input type=submit value=Check>
</form>

How can I do it? I do not know where to put what. Hope someone can help me out here. Thanks

james_cwy
12-10-2003, 11:30 PM
I tried using a simpler code:
function doanothercheck() {
var incount=<?php echo $num;?>;
for (i = 1; i <= incount; i++)
{
if(document.xyz["pcname"+i].value.length < 1) {
alert("Please enter a name.");
return false;
}
else {
return true;
}//close else
}//close for
}//close function

but the problem is-it can check only the first pcname1 but the rest it cannot check.Any ideas?

ray326
12-11-2003, 12:40 PM
Logic error (and slight enhancement):

for (i = 1; i <= incount; i++)
{
if(document.xyz["pcname"+i].value.length < 1) {
alert("Please enter a name.");
document.xyz["pcname"+i].focus();
return false;
}
}//close for
return true;

james_cwy
12-11-2003, 08:37 PM
Thanks alot ray. I used the code that you had proposed and it worked fine.Thanks alot.Appreciate your help