Remove the "$" before the ASP.Net RangeValidator script is executed

How can I get the replace script input to run before the RangeValidator script?

I want to prevent validation from failing when the user enters a dollar sign or a comma.

function cleanUp(str) {
    re = /^\$|,/g;
    return str.replace(re, ""); // remove "$" and ","
}

<input type="text" id="salary" runat="server"
onchange="this.value=cleanUp(this.value)" />

<asp:RangeValidator ID="salaryValidator" 
    runat="server" ErrorMessage="Invalid Number"
    ControlToValidate="salary" Type="Double" />

      

UPDATE:
I decided to use a CustomValidator which validates the range and uses the RegEx currency. Thank you Michael Kiskern.

function IsCurrency(sender, args) {
    var input = args.Value;

    // Check for currency formatting.
    // Expression is from http://regexlib.com/REDetails.aspx?regexp_id=70
    re = /^\$?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(.[0-9][0-9])?$/;
    isCurrency = input.match(re);

    if (isCurrency) {
        // Convert the string to a number.
        var number = parseFloat(CleanUp(input));
        if (number != NaN) {
            // Check the range.
            var min = 0;
            var max = 1000000;
            if (min <= number && max >= number) {
                // Input is valid.
                args.IsValid = true;
                return;
            }
        }
    }

    // Input is not valid if we reach this point.
    args.IsValid = false;
    return;
}

function CleanUp(number) {
    re = /^\$|,/g;
    return number.replace(re, ""); // remove "$" and ","
}       

<input type="text" id="salary" runat="server" />

<asp:CustomValidator ID="saleryValidator" ControlToValidate="salary" runat="server" 
ErrorMessage="Invalid Number" ClientValidationFunction="IsCurrency" />

      

+1


source to share


4 answers


Have you tried using the CustomerValidator control and combined the functionality of the JS cleanup methods and the RangeValidator.



+3


source


I think I can improve this. This makes commas and cents optional:



^\$?([0-9]{1,3},?([0-9]{3},?)*[0-9]{3}|[0-9]+)(\.[0-9]{0,2})?$

      

+3


source


There is a way to do this by registering a script; however, why not use a regular expression tool to validate your input?

Also, the range checker runs on the fields onBlur js event, not on change.

0


source


Just noticed that you have. for the decimal point, but that means the regex will accept any character in that place. You must use \.

for this decimal point.

/^\$?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(\.[0-9][0-9])?$/

      

0


source







All Articles