Since you said it'd have to be a quantity I'd also use the IsNumeric function to test it after you test if something is in the textbox... Like:
Main Topics
Browse All TopicsI have been looking for several hours trying to find code for requiring a customer to enter qty in the qty_field when a checkbox is checked. Here's what I am trying to do:
i have several test available, so when customers checked on Checkbox1(or Checkbox2); they must also enter a qty for the qty_field. The qty_field is used for calculation later. I try ASP.net 2.0 Validation, but it does not have option for me to tell it that if checkbox1 is checked, then qty1 must also cannot be empty. I am storing my values using session. what i have below does not work (and no error message), just no result.
How do I validate the checkbox and the textbox before submitting the form?
thanks
my aspx code:
<form id="form2" runat="server">
<div>
<table cellpadding="4" cellspacing="0" width="100%" border="1">
<tr>
<td width="40%">
<asp:CheckBox ID="CheckBoxLT1" Text="Test1" runat="server" />
</td>
<td width="10%">
<asp:TextBox ID="txtltNumTest" runat="server" Width="70px"></asp:TextBox
</td>
</tr>
<tr> <td width="40%"><asp:CheckBox ID="CheckBoxLT2" Text="Test2" runat="server" />
</td> <td width="10%"><asp:TextBox ID="txtltNumTest2" runat="server" style="height: 22px"
Width="70px"></asp:TextBox
</td> </tr>
</table>
<asp:Button ID="Button1" runat="server" Text="Submit" />
</div>
</form>
aspx.vb code:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'check to see which test is checked
'check to see which test is checked
If CheckBox1.Checked = True Then
If AntiNumTest.Text <> "" Then
Session("testqty") = CInt(AntiNumTest.Text)
Session("anticost") = Session("testqty") * 250
Else
response.write ("Please Enter qty for test")
End If
End If
Session("Cost") = Session("anticost")
Response.Redirect("page2.a
End Sub
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Or you can use
If AntiNumTest.Text = "" Then
'Nothing in box
Else
If IsNumeric(AntiNumTest.Text
'It's a number
Else
'It's not a number
End If
End If
Or
If AntiNumTest.Text.ToString.
'Nothing in box
Else
If IsNumeric(AntiNumTest.Text
'It's a number
Else
'It's not a number
End If
End If
and you can use this javascript so no one inputs anything but a number
<script type="text/javascript" language="javascript">
function clearText(field) {
if (field.defaultValue == field.value) field.value = '';
else if (field.value == '') field.value = field.defaultValue;
}
function isNumberKey(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
</script>
If you want to validate the checkbox and textbxo before postback then you can add a requiredfieldvalidator control on the textbox and enable/disable it according to whether the checkox is ticked or not.
for example:
the aspx page
------------------
<html xmlns="http://www.w3.org/1
<head runat="server">
<title></title>
<script type="text/javascript">
function ValidatorEnabled() {
var checkbox = document.getElementById('<%
ValidatorEnable(document.g
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBox ID="checkbox1" runat="server" Text="Test" />
<asp:TextBox ID="textbox1" runat="server" />
<asp:RequiredFieldValidator
<asp:Button ID="button1" runat="server" Text="submit" />
</div>
</form>
</body>
</html>
Notice the requiredfieldvalidator control is initially disabled (Enabled="false"), I've done it this way since the initial value of the checkbox in this example is not checked, so no need for the validation.
Then in the codebehind add an event handler to the checkbox control in the Page_Load...
checkbox1.Attributes.Add("
The javascript function is called each time the checkbox is clicked on and it enables the validator if the checkbox is ticked and disables it if it's unticked.
The result is if you check the checkbox you can't submit the form without putting text into the textbox, but if you untick the checkbox you can submit the form if the textbox is empty or if it has something in it.
Business Accounts
Answer for Membership
by: arhamePosted on 2009-11-03 at 14:04:20ID: 25734299
If AntiNumTest.Text.ToString. Trim.Lengt h = 0 Then
'Nothing in box
Else
'Something in box
End If