By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,795 Members | 1,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,795 IT Pros & Developers. It's quick & easy.

How to validate multiple dynamically named form fields?

P: n/a
I am needing to determine how to go about validating that a field in
my form contains only a positive integer. I know that this is fairly
simple if the form contains only one element to be validated; but, a
much bigger challenge ( to me anyway, that's why I'm coming to the
pros! ) when I don't know exactly how many fields may appear on the
form as it is dynamically generated based upon the number of line
items to be received on a purchase order. (i.e. if there are 10 items
to receive in; I will have 10 text fields to enter "quantity received"
into. Obviously, I want to be sure that only a positive integer is
accepted into these fields.

I apologize for pasting in SO MUCH of my code, but it gives you the
insight as to how my form elements are getting named...

The field I'm needing to validate is referenced in the code below as:
<td><input type="text" size="7" maxlength="6" name="<?php echo
'amtReceived_'.$poLineItems[VItemID]; ?>" value="" /></td>

Here is the code that generates the dynamic number of table rows/form
elements:
<?php
while ($poLineItems = mysql_fetch_assoc($result)) {
?>
<tr class="lineitem">
<td><?= $poLineItems[VItemID] ?></td>
<td><?= $poLineItems[CPN] ?><input type="hidden" name="<?php
echo 'cpn_'.$poLineItems[VItemID]; ?>" value="<?= $poLineItems[CPN] ?
>" /></td>
<td align="left"><?= $poLineItems[CDescription] ?></td>
<td><?= $poLineItems[OnOrder] ?></td>
<td><?= $poLineItems[OnHand] ?></td>
<td><input type="text" size="7" maxlength="6" name="<?php echo
'amtReceived_'.$poLineItems[VItemID]; ?>" value="" /></td>
<td>
<select name="<?php echo 'location_'.$poLineItems[VItemID]; ?>">
<?php

$stmt2 = "SELECT DISTINCT City, Zip FROM shipto WHERE CompanyID
= 169";
$result2 = mysql_query($stmt2);

while($rowDD = mysql_fetch_row($result2)) {
if($rowDD[0] == 'Rosemont') {
echo '<option value="'.$rowDD[0].'" selected>'.
$rowDD[0].', '.$rowDD[1].'</option>';
} else {
echo '<option value="'.$rowDD[0].'">'.$rowDD[0].', '.
$rowDD[1].'</option>';
}
}
?>
</select>
</td>
</tr>
<?php
}
?>
</table>
<p><input type="submit" value="Receive Items" method="POST" /></p>
<?php
} else {
?>

<p><div class="nogo">No items found for PO number <?=
$receive_ponum ?</div><br />
<input type="button" value="<< Go Back <<"
onclick="history.back()" />
</p>
<?php
}
?>

Thanks for any help whatsoever......

Gene

Apr 4 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"phpCodeHead" <ph*********@gmail.comwrote in message
news:11*********************@b75g2000hsg.googlegro ups.com...
>I am needing to determine how to go about validating that a field in
my form contains only a positive integer. I know that this is fairly
simple if the form contains only one element to be validated; but, a
much bigger challenge ( to me anyway, that's why I'm coming to the
pros! ) when I don't know exactly how many fields may appear on the
form as it is dynamically generated based upon the number of line
items to be received on a purchase order. (i.e. if there are 10 items
to receive in; I will have 10 text fields to enter "quantity received"
into. Obviously, I want to be sure that only a positive integer is
accepted into these fields.

I apologize for pasting in SO MUCH of my code, but it gives you the
insight as to how my form elements are getting named...
<snip PHP code mostly>

None of your PHP code has anything to do with JavaScript.

The only thing that matters is you address your form's elements collection. For example:

<form name="form1">
<input type="text" name="text1" />
<input type="text" name="text2" />
<input type="submit" value="Submit" />
</form>

document.forms['form1'].elements is a collection of all elements within that form. You
could then iterate those elements, excluding submit, and check your inputs values.

You might use parseInt() and check whether or not it is less than 1 (or 0, depending upon
what you consider negative).

As a last aside, I would not mix traditional PHP start and end tags with ASP or ECHO-style
tags.

-Lost
Apr 4 '07 #2

P: n/a
On Apr 5, 9:22 am, "-Lost" <missed-s...@comcast.netwrote:
"phpCodeHead" <phpcodeh...@gmail.comwrote in message

news:11*********************@b75g2000hsg.googlegro ups.com...
I am needing to determine how to go about validating that a field in
my form contains only a positive integer. I know that this is fairly
simple if the form contains only one element to be validated; but, a
much bigger challenge ( to me anyway, that's why I'm coming to the
pros! ) when I don't know exactly how many fields may appear on the
form as it is dynamically generated based upon the number of line
items to be received on a purchase order. (i.e. if there are 10 items
to receive in; I will have 10 text fields to enter "quantity received"
into. Obviously, I want to be sure that only a positive integer is
accepted into these fields.
I apologize for pasting in SO MUCH of my code, but it gives you the
insight as to how my form elements are getting named...

<snip PHP code mostly>

None of your PHP code has anything to do with JavaScript.

The only thing that matters is you address your form's elements collection. For example:

<form name="form1">
<input type="text" name="text1" />
<input type="text" name="text2" />
<input type="submit" value="Submit" />
</form>

document.forms['form1'].elements is a collection of all elements within that form. You
could then iterate those elements, excluding submit, and check your inputs values.

You might use parseInt() and check whether or not it is less than 1 (or 0, depending upon
what you consider negative).
parseInt is ineffective when checking that only digits have been input
since it will ignore trailing non-digit characters. Much better to
use a regular expression, e.g.:

To check that a string contains zero or more digits, and only digits:

function checkOnlyDigits( n ){
return !/\D/.test(n);
}
To check that it is a non-negative integer, test that it contains one
or more digits, and only digits:

function validateIntegerNN( n ){
return /^\d+$/.test(n);
}

There are many more examples on JRS's site:

<URL: http://www.merlyn.demon.co.uk/js-valid.htm#VNP >
For the OP, an example implementation (purely a trivial example, a
more sophisticated validation routine should be employed in
production) to check for a non-negative integer (i.e. zero or higher)
is:

<script type="text/javascript">

function validateIntegerNN( n ){
return /^\d+$/.test(n);
}

</script>

<input type="text" onblur="alert( validateIntegerNN(this.value) );">
The alert will show 'true' only if the input contains at least one
digit, and only digits, when it loses focus. Otherwise, it will show
false.

The exception is that valid integers such as 3e3 (3000) will be
rejected.
--
Rob

Apr 5 '07 #3

P: n/a
On Apr 5, 8:42 am, "RobG" <r...@iinet.net.auwrote:
>
For the OP, an example implementation (purely a trivial example, a
more sophisticated validation routine should be employed in
production) to check for a non-negative integer (i.e. zero or higher)
is:

<script type="text/javascript">

function validateIntegerNN( n ){
return /^\d+$/.test(n);
}

</script>

<input type="text" onblur="alert( validateIntegerNN(this.value) );">

The alert will show 'true' only if the input contains at least one
digit, and only digits, when it loses focus. Otherwise, it will show
false.

The exception is that valid integers such as 3e3 (3000) will be
rejected.
How about:
/^\d+(e\d+)?$/i

Apr 5 '07 #4

P: n/a
On Apr 5, 1:04 pm, "Cah Sableng" <cahsabl...@gmail.comwrote:
[...]
How about:
/^\d+(e\d+)?$/i
Fill yer boots :-)

The link I provided has lots of examples, I just wanted to point out a
restriction of the code I posted.
--
Rob

Apr 5 '07 #5

P: n/a
In comp.lang.javascript message <11**********************@d57g2000hsg.go
oglegroups.com>, Wed, 4 Apr 2007 18:42:51, RobG <rg***@iinet.net.au>
posted:
>
The exception is that valid integers such as 3e3 (3000) will be
rejected.
!(S%1) looks OK at first sight for testing whether Number(S) has an
integer value :-) unless S amounts to NaN :-( .

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Apr 5 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.