473,396 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to validate multiple dynamically named form fields?

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
5 7119
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: PT | last post by:
I got a problem. And thats..... First of all, I got these three tables. ------------------- ------------------ ---------------------- tblPerson tblPersonSoftware ...
1
by: Randell D. | last post by:
HELP! I am determined to stick with this... I'm getting there... for those who haven't read my earlier posts, I'm createing what should be a simple function that I can call to check that...
7
by: bu | last post by:
I have a form with a handful of comments fields. I am trying to code the form in such a way that when the user clicks on the field, a dialog box will open up displaying the full contents of the...
12
by: shank | last post by:
I'm trying to use online samples for submitting multiple records from ASP into a stored procedure. Failing! Through the below form, a user could be submitting many records at a time. I'm not...
2
by: Sethos | last post by:
I am sure that this has been covered, hashed, and rehashed, but a search on the group did not produce the answer, so forgive me if this seems like a "newbie" type question... Besically, I have a...
1
by: Shizbart | last post by:
MS Access 97 Beginner/Moderate Level User I am trying to create a Database to track Workouts in MS Access 97. I have one Table named Workouts that contains the following Fields: Workout...
2
by: Neo Geshel | last post by:
Greetings, I have a form with a telephone field. It is very specific, as it has four text boxes - the country code, area code, prefix and suffix. I can validate each of them individually, but...
1
by: SkipNRun | last post by:
I am a novice when comes to JavaScript, AJAX. I am working on a form, which will allow users to update their contact information. In order to make the form flexible, I need to use pull down list. ...
7
by: john.cole | last post by:
I have searched all the groups I can, and I still haven't been able to come up the solution I need. I have the following problem. In my form named sbfrmSpoolList, I am entering a job, spool and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.