Stock Options

ASP.NET C # Validate only part of the form if checked

Given:

<div class="subHead">Stock Options</div> <table class="settingTable"> <tr> <td colspan="2"><b>Limited Stock</b></td> </tr> <tr> <td width="50" align="center"><asp:CheckBox ID="limitedStock" runat="server" /></td> <td>If checked this product will have a limited stock</td> </tr> </table> <table class="settingTable"> <tr> <td colspan="2"><b>Stock Count</b></td> </tr> <tr> <td> <asp:TextBox ID="stockCount" runat="server" CssClass="tbox"></asp:TextBox> <asp:RequiredFieldValidator runat="server" id="RequiredFieldValidator2" ControlToValidate="stockCount" ErrorMessage="You need to enter a value" display="Dynamic" /> <asp:RangeValidator runat="server" id="rangeVal1" MinimumValue="0" MaximumValue="999999999999" ControlToValidate="stockCount" ErrorMessage="Enter a numeric value of at least 0" display="Dynamic" /> </td> </tr> </table> 

How can I make sure that the inventory calculator does not start if the limited fund check box is not selected?

+4
source share
2 answers

Use CustomValidator instead. See the "Client Side Validation" section at the bottom of this page: http://msdn.microsoft.com/en-us/library/f5db6z8k%28VS.71%29.aspx

You can use a script that checks the value of the checkbox and does your check.

 <script language="text/javascript"> function validateStockCount(oSrc, args){ //Use JQuery to look for the checked checkbox and only if it is found, validate if($('.limitedStock:checked') == undefined) { args.IsValid = true; } else { args.IsValid = (args.Value.length >= 0) && (args.Value.length <= 999999999999); } } </script> <asp:CheckBox ID="limitedStock" runat="server" CssClass="limitedStock" /> <asp:TextBox ID="stockCount" runat="server" CssClass="tbox"></asp:TextBox> <asp:CustomValidator id="CustomValidator1" runat=server ControlToValidate = "stockCount" ErrorMessage = "You need to enter a numeric value of at least 0!" ClientValidationFunction="validateStockCount" > </asp:CustomValidator> 
+3
source

You can set the value of AutoPostBack in the true field, and then in the checkbox field you can enable / disable the required field validation module.

On an aspx page, set the AutoPostBack property of the checkbox

 <asp:CheckBox ID="limitedStock" runat="server" AutoPostBack="True" /> 

In the CheckChanged event of this flag, you simply set the Enabled RequiredFieldValidator property as follows:

 RequiredFieldValidator2.Enabled = limitedStock.Checked; 

James :-)

+2
source

All Articles