asp.nethtmlwebformsrangevalidator

How to make the RangeValidator on an input of type "number" work in all localizations?


The RangeValidator will fetch the value of the input and validate it with regex. Part of the regex is the localized decimal character.

So, in the German culture the RangeValidator will fail if I enter 100.00, however it will accept 100,00, because , is the decimal character of the German culture.

On the new input[type=number] (asp:TextBox with TextMode="Number") of HTML5, I can enter 100,00 or 100.00 (with the stepattribute set to "0.01" and it will always hold the value 100.00. This will cause the RangeValidator to fail all the time in the German culture.

Changing the location to en-US would solve the problem, but I need the application in the users locale.

Can I, somehow, tell the RangeValidator to always use . as decimal character or are there any better ways to handle this situation?


Solution

  • So this is dirty but works:

    I've overridden the ValidatorValidate function of the framework:

    function ValidatorValidate(val, validationGroup, event) {
        // new code: 
        if (typeof (val.customized) == "undefined") {
            var data = $(val).data();
            for (var key in data) {
                val[key] = data[key];
            }
            val.customized = true;
        }
    
        // this code was copied from the framework:
        val.isvalid = true;
        if ((typeof (val.enabled) == "undefined" || val.enabled != false) && IsValidationGroupMatch(val, validationGroup)) {
            if (typeof (val.evaluationfunction) == "function") {
                val.isvalid = val.evaluationfunction(val);
                if (!val.isvalid && Page_InvalidControlToBeFocused == null &&
                        typeof (val.focusOnError) == "string" && val.focusOnError == "t") {
                    ValidatorSetFocus(val, event);
                }
            }
        }
        ValidatorUpdateDisplay(val);
    }
    

    And now I can force specific JS variables on the validator by using data attributes. To force the RangeValidator to use the en-US locale, I simply force it to set decimalchar to . and digits to 2 from the ASPX markup:

    <asp:RangeValidator ID="rv" runat="server" Data-DecimalChar="." Data-Digits="2" MinimumValue="-100" MaximumValue="100" ControlToValidate="tb" Type="Currency" />
    

    But, this is just dirty, it's not? ...

    Please be aware of problems with UpdatePanels when overriding JS functions that involve the client validation mechanism, like this one.

    What I'm really confused about is the fact this works, even when I'm only modifying the client side script. The server sided validation accepts the value from the English culture. This is really strange.