473,785 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_ass oc($result)) {
?>
<tr class="lineitem ">
<td><?= $poLineItems[VItemID] ?></td>
<td><?= $poLineItems[CPN] ?><input type="hidden" name="<?php
echo 'cpn_'.$poLineI tems[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_'.$po LineItems[VItemID]; ?>">
<?php

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

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="histor y.back()" />
</p>
<?php
}
?>

Thanks for any help whatsoever..... .

Gene

Apr 4 '07 #1
5 7144
"phpCodeHea d" <ph*********@gm ail.comwrote in message
news:11******** *************@b 75g2000hsg.goog legroups.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.ne twrote:
"phpCodeHea d" <phpcodeh...@gm ail.comwrote in message

news:11******** *************@b 75g2000hsg.goog legroups.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 validateInteger NN( 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 validateInteger NN( n ){
return /^\d+$/.test(n);
}

</script>

<input type="text" onblur="alert( validateInteger NN(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 validateInteger NN( n ){
return /^\d+$/.test(n);
}

</script>

<input type="text" onblur="alert( validateInteger NN(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...@gma il.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.javas cript message <11************ **********@d57g 2000hsg.go
oglegroups.com> , Wed, 4 Apr 2007 18:42:51, RobG <rg***@iinet.ne t.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.c om/faq/index.html>.
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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
8750
by: PT | last post by:
I got a problem. And thats..... First of all, I got these three tables. ------------------- ------------------ ---------------------- tblPerson tblPersonSoftware tblSoftware ------------------- ------------------ ---------------------- PID PName PID* SID* SID SWName --- ----- --- --- --- ------ 1 Thomas 1 1 ...
1
6027
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 required fields in a form have values. I'm writing the values to the client using document.write only so that I can confirm that the values are there to be played with - this is not the final result so please (unless it's leading to the script failure)...
7
2113
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 field. I want to reuse the same window for each field ( seems sensless to code a screen n times for n fields when the only thing that is really changing is the source data ), but am having all sorts of issues. I have tried dynamically setting the...
12
3012
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 getting any records inserted. For troubleshooting, I cut the form down to 1 textbox and when submitted it populated 5 rows of the same data. So I know I'm connected, but not getting the LOOP and NEXT correct? How do I set this up so many records can be...
2
2137
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 form on which the users can click on a button to add text boxes dynamically. That all works without a hitch. The problem comes in trying to verify the information in the boxes. Below is a very abbreviated example of the code I have in the form:
1
3053
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 Code (e.g: U100), WorkoutActivity (e.g.: Bench Press: Flat Bench Straightbar) (note: this is the Primary Key of this Table), and Target Area (e.g.: Upper Body / Chest). A second Table named Workout_Log contains the following Fields: Date,...
2
2395
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 I am stumped as to how to validate them as a group (as one final validation). I need to check to see if all (at one time) are filled or empty. The other individual validations handle cases where the text boxes are filled with letters or ...
1
4007
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. Depends on the pull down list selection, I use script.aculo.us to validate the user input before submit and pass the necessary data, such as contact type, contact information and ranking to a php program for updating. This form should allow multiple...
7
6864
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 revision number. My table is indexed properly to not allow duplicates, but I would like teh user to be notified that they are typing a duplicate via a message box, then I woulld the update of the record to be cancelled. I have tried the...
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9481
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10095
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9953
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7502
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5383
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3655
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.