javascript - How to validate the summation of textboxes values in the client side -
if have group of textboxes group 1 , , group of textboxes group 2 .both of them in edit template of grid view want validate the summation of first group equal summation of second group in client side allow save or add .
ex:
txt1 txt2 txt3 txt4
i want validate :
decimal.parse(txt1.text)+ decimal.parse(txt2.text) = decimal.parse(txt3.text )+ decimal.parse(txt4.text)
note:
one or more of these text boxes may empty , in case consider value
0
how thing using asp.net validators .
you can use customvalidators. https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.aspx
below little example started. can expand mycustomvalidation
include checking non-integer values, min-max ranges etc.
<asp:textbox id="textbox1" runat="server" validationgroup="mygroup"></asp:textbox> <br /> <asp:textbox id="textbox2" runat="server" validationgroup="mygroup"></asp:textbox> <br /> <asp:textbox id="textbox3" runat="server" validationgroup="mygroup"></asp:textbox> <br /> <asp:textbox id="textbox4" runat="server" validationgroup="mygroup"></asp:textbox> <br /><br /> <asp:button id="button1" runat="server" text="button" validationgroup="mygroup" /> <br /> <asp:customvalidator id="customvalidator1" runat="server" errormessage="error adding textbox values" validationgroup="mygroup" clientvalidationfunction="mycustomvalidation"></asp:customvalidator> <script type="text/javascript"> function mycustomvalidation(osrc, args) { var tb1 = document.getelementbyid('<%=textbox1.clientid %>').value; var tb2 = document.getelementbyid('<%=textbox2.clientid %>').value; var tb3 = document.getelementbyid('<%=textbox3.clientid %>').value; var tb4 = document.getelementbyid('<%=textbox4.clientid %>').value; if (tb1 == "" || tb2 == "" || tb3 == "" || tb4 == "") { //to make sure validator fires if 1 or more textboxes left empty args.isvalid = false; } else if (((tb1 + tb2) == (tb3 + tb4))) { args.isvalid = true; } else { args.isvalid = false; } } </script>
Comments
Post a Comment