Click to See Complete Forum and Search --> : Validating set of checkboxes & text boxes in loop
shanuragu
08-25-2003, 12:16 AM
Hi
I have displayed chk boxes & text boxes with in while loop. Each txt box has respective chk box. When a chk box is clicked corresponding text box should have a value, it should not be empty.
The code is as follows
<td width="7%" align="center" bgcolor="<%=bgColor%>">
<input type="text" id="qty_ordered_<%=rsItems("item_id")%>" size="3" name="qty_ordered_<%=rsItems("item_id")%>" >
</td>
<td width="10%" align="center" bgcolor="<%=bgColor%>"><input type="checkbox" id="add_item_<%=rsItems("item_id")%>" name="add_item" value="<%=rsItems("item_id")%>"></td>
</tr>
<%
rsItems.MoveNext
Wend
%>
How can I validate the above text box.
shara
Try some code like this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Checkbox validation</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function validate(frm) {
for (i=0;i<frm.elements.length;i++) {
if (frm.elements[i].type == "checkbox") {
if (frm.elements[i].checked) {
name = frm.elements[i].name;
ending = name.split("_");
elem = "input_"+ending[1];
if(/^\s*$/.test(frm.elements[elem].value)) {
msg = "Textbox(s) must contain a value if you check the checkbox";
}
}
}
}
if (msg) {
alert (msg);
return false;
}
else {
return true;
}
}
</script>
</head>
<body>
<form name="myform" action="" onsubmit="return validate(this);">
<p><input type="text" name="input_01"> <input type="checkbox" name="checkbox_01">
<input type="text" name="input_02"> <input type="checkbox" name="checkbox_02">
<input type="text" name="input_03"> <input type="checkbox" name="checkbox_03">
<input type="submit" value="submit"></p>
</form>
</body>
</html>
shanuragu
08-26-2003, 07:09 AM
This works fine with chk boxes which are displayed in an order like chk_1, chk_2 etc. But in my case I get the chk box values from the database which will never be in an order (ex. chk_3, chk_6,chk_7) & the corresponding text box names too will have the same values as chk boxes (ex.txt_3, txt_6, txt_7) .
Any solution for the above problem.
shara
Seems to work fine to me:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Checkbox validation</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function validate(frm) {
for (i=0;i<frm.elements.length;i++) {
if (frm.elements[i].type == "checkbox") {
if (frm.elements[i].checked) {
name = frm.elements[i].name;
ending = name.split("_");
elem = "input_"+ending[1];
if(/^\s*$/.test(frm.elements[elem].value)) {
msg = "Textbox(s) must contain a value if you check the checkbox";
}
}
}
}
if (msg) {
alert (msg);
return false;
}
else {
return true;
}
}
</script>
</head>
<body>
<form name="myform" action="" onsubmit="return validate(this);">
<p><input type="text" name="input_03"> <input type="checkbox" name="checkbox_03">
<input type="text" name="input_09"> <input type="checkbox" name="checkbox_09">
<input type="text" name="input_07"> <input type="checkbox" name="checkbox_07">
<input type="submit" value="submit"></p>
</form>
</body>
</html>