Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

validate only the selected record

Status
Not open for further replies.

deadfish

Programmer
Nov 13, 2002
50
0
0
HK
Hi,

I have a form that is dynamically generated from the db like this:

item1 checkbox1 qty1
item2 checkbox2 qty2
item3 checkbox3 qty3

I have to verify the qty fields to ensure they are positive integer only when the corresponding checkbox checked. For example, if checkbox1 has been checked, then I need to check whether qty1 is an integer greater than 0. No validation for qty1 is need if the checkbox1 is not checked. How to do this?

Thanks!
 
When you build the field with the quantity then make the name for that field sequential such as item1, item2, item3...
When you build the checkbox insert an onclick="myfunction('1');

Something like this:


<html>
<head>
<title></title>
<script type="text/javascript">
function myfunction(which)
{
var whichfield = "C" + which;
if (document.all(whichfield).checked)
{
alert(document.all("T"+which).value);
}
}
</script>
</head>
<body>
<form method="POST" action="">
<input
type="checkbox" name="C1" value="ON" onclick="myfunction('1');"><input type="text"
name="T1" size="20"><br>
<input type="checkbox" name="C2" value="ON" onclick="myfunction('2');"><input type="text"
name="T2" size="20"><br>
<input type="checkbox" name="C3" value="ON" onclick="myfunction('3');"><input type="text"
name="T3" size="20"></p>
<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset"
name="B2"></p>
</form>
</body>
</html>

Note that I test if the calling box is checked so that it does not do anyting in the case that the user UN-checked that box. Someone else can probably show an inline way of deteching this before firing onclick though.
 
i believe you would use something like
Code:
<input type="checkbox" name="checkbox1" value="On">

If (document.formname.checkbox1.value=="On" && document.formname.qty1>0){stuff to do}
 
I knew it could be done so I HAD to figure it out.

Try the code below. I put the test to see if it was checked inline with the onclick. This way your test function ONLY gets called if the box has just been checked.
Seems cleaner that way.


<html>
<head>
<title></title>
<script type="text/javascript">
function myfunction(which)
{
alert(document.all("T"+which).value);
}
</script>
</head>
<body>
<form method="POST" action="">
<input type="checkbox" name="C1" value="ON" onClick="if (this.checked) {myfunction('1')}">
<input type="text" name="T1" size="20"><br>
<input type="checkbox" name="C2" value="ON" onClick="if (this.checked) {myfunction('2')}">
<input type="text" name="T2" size="20"><br>
<input type="checkbox" name="C3" value="ON" onClick="if (this.checked) {myfunction('3')}">
<input type="text" name="T3" size="20">
<input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>
</body>
</html>


Paranoid? ME?? WHO WANTS TO KNOW????
 
Thanks!
But can I have the same name for all checkbox but with different values?

I mean:
<input type="checkbox" name="C" value="1" onclick="myfunction('2');"><input type="text"
name="T2" size="20"><br>
<input type="checkbox" name="C" value="2" onclick="myfunction('3');"><input type="text"
name="T3" size="20"></p>
.....
 
Yes, you can use the same name for all the checkboxes.
It is bad form though and might even cause errors in some browsers. I do not think it will make much difference for this use though.

But you MUST have a unique name for each of the input fields with your values in them or you have no way to reference them in the function so that you can test it's value. This is not entirely true, you CAN have duplicate names but it will make the means of testing the field values more complex for you.

As far as the checkbox names, you need to generate the number you pass in the onclick function anyway as well as append that same number to the end of the name of the item field so it is just as easy to append the same number to the name of the checkbox.

Show me the code you use to generate your form and I could give you a more specific example.
And do you already have a function to test the values?




Paranoid? ME?? WHO WANTS TO KNOW????
 
I need to use the same name for all checkbox because I need to get the qty fields in ASP code by using the code:

For Each Item In Request.Form("C")
......


Just don't know how to make it work.....
 
Yes, you can use the same name for all the checkbox fields, just make sure that the names for the input fields are sequential like item1, item2, item3.

If you name your fields the same thing such as making three fields named MyInputfield, then you would have to iterate through them like an array and check their values like: MyInputfield[0].value, MyInputfield[1].value, etc.

In this case the onclick statement is inline with the checkbox and it tests against itself with "this.checked" so you never have to look up the checked property elsewhere.

Have you tried the above code to see if it works in your script?


Paranoid? ME?? WHO WANTS TO KNOW????
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top