1

I wrote a decimal validation method to validate the text in a texbox:

$.validator.addMethod("decimalCheck", function(value) {
            var v = new RegExp("^\d*[0-9](\.\d*[0-9])?$", "g");
            return v.test(value);
        }, "Error here");

The first time, I inputed "12,34" string to textbox and error message display. The second time, I inputed "12" string to textbox. That is valid string but error message doesn't hide.

Please help me resolving thanks & best regards

1 Answer 1

3

Try this one (also slightly optimized regex with same functionality)

$.validator.addMethod("decimalCheck", function(value) {
    var v = /^\d+(\.\d+)?$/;
    return v.test(value);
}, "Error here");

Your problem is that you need to escape the backslash it self (double escape) else the regex constructor in reality get this string passed in ("^d*[0-9](.d*[0-9])?$")

var v = new RegExp("^\\d*[0-9](\\.\\d*[0-9])?$", "g");

btw. using the g flag here could lead to unexpected results. Better leave it away as you are using ^ and $ anyway.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.