1

I have a VB web app built in .Net 4.0 and am trying to add some custom validation.

I have 6 asp textboxes that I want to require the user to sum to 100. All 6 fields have required field and required expression validators that require the user to enter a number between 0.0 and 100.0, but I need additional validation to sum to 100.0. So again, 6 textboxes with numbers only that allow whole numbers or single decimal numbers.

I'm a relatively new programmer, what's the best way to do this:

Textbox1 + Textbox2 + Textbox3 + Textbox4 + Textbox5 + Textbox 6 = 100.0 (if not, prompt user with values don't equal 100.0 and don't allow button click until the values are fixed.

Thanks for your help!

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
pmlevere
  • 77
  • 10

1 Answers1

3

Use a CustomValidator which is the only validator that allows to omit the ControlToValidate:

<asp:CustomValidator runat="server" ID="CustomValidator1"
   Text="The sum must be 100" 
   ClientValidationFunction="clientValidate" 
   EnableClientScript="True"
   Display="Static">
</asp:CustomValidator>

The ServerValidate:

Protected Sub ServerValidation(source As Object, args As ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
    Dim val1 As Decimal
    Dim val2 As Decimal
    Dim val3 As Decimal
    Dim val4 As Decimal
    Dim val5 As Decimal
    If Decimal.TryParse(TextBox1.Text, val1) AndAlso _
       Decimal.TryParse(TextBox2.Text, val2) AndAlso _
       Decimal.TryParse(TextBox3.Text, val3) AndAlso _
       Decimal.TryParse(TextBox4.Text, val4) AndAlso _
       Decimal.TryParse(TextBox5.Text, val5) Then
        args.IsValid = val1 + val2 + val3 + val4 + val5 = 100
    Else
        args.IsValid = False
    End If
End Sub

You can also provide a client validation function. Therefore you need to find the references to the textboxes per javascript and then calculate the total value on clientside.

For example:

<script type="text/javascript" >
    function clientValidate(source, arguments) {
        var txt1 = document.getElementById('<%= TextBox1.ClientID %>');
        var txt2 = document.getElementById('<%= TextBox2.ClientID %>');
        var txt3 = document.getElementById('<%= TextBox3.ClientID %>');
        var txt4 = document.getElementById('<%= TextBox4.ClientID %>');
        var txt5 = document.getElementById('<%= TextBox5.ClientID %>');
        if(txt1 != null && txt2 != null && txt3 != null && txt4 != null && txt5 != null && txt1.value != "" && !isNaN(txt1.value) && txt2.value != "" && !isNaN(txt2.value) && txt3.value != "" && !isNaN(txt3.value) && txt4.value != "" && !isNaN(txt4.value) && txt5.value != "" && !isNaN(txt5.value))
        {
            var num1 = parseFloat(txt1.value);
            var num2 = parseFloat(txt2.value);
            var num3 = parseFloat(txt3.value);
            var num4 = parseFloat(txt4.value);
            var num5 = parseFloat(txt5.value);
            arguments.IsValid = num1 + num2 +num3 + num4 + num5 == 100;
        }
        else
            arguments.IsValid = false;
    }
</script>
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Seems like the client validation might offer the most functionality, but for the moment i'm trying the top piece of code. A quick question, and i feel like an idiot asking it, how do i activate the custom validator? I've added the custom validator to my aspx page and added it into the validation group that is validated on my button click for the data in the textboxes, and i've added the code into my vb page. Shouldn't adding the custom validator to my validation group that also runs my required field and regular expressions validators cause it to run when my button is clicked? – pmlevere Sep 30 '13 at 16:10
  • @pmlevere: The client validation function is just a nice-to-have. The most important method is the server-validate since javascript could be disabled at all. You always have to validate also on serverside. According to your problems with the validator. Have a look at a former answer of mine on a related question: http://stackoverflow.com/a/10172624/284240 – Tim Schmelter Sep 30 '13 at 16:14
  • You should really validate on both client and server side. If it fails client-side validation, don't even bother sending it to the server. But you absolutely have to have the server-side validation because the client-side validation is easily circumvented. – Douglas Barbin Sep 30 '13 at 16:34