reflexiv
reflexiv

Reputation: 1723

JQuery validation plugin regex validate only matches

Yes, yet another question on regex with the JQuery validation plugin. I've followed all the examples I've found but for some reason cannot get this to validate only regex matches. It validates everything regardless of what regex I use (going for dollar amount under 100, optional $ and optional decimal up to hundredths place):

$.validator.addMethod("regexp", function (value, element) {
    return this.optional(element) || /^\$?\d{0,2}(\.\d{1,2}$)?/.test(value);
}, 'Please enter a valid USD format under $100, dollar sign optional.');

$("#formname").validate({
    debug: true,
    rules: {
        textbox: {
            required: true,
            regexp: true
        }
    }
});

JSFIDDLE DEMO

Upvotes: 1

Views: 4810

Answers (1)

Anirudha
Anirudha

Reputation: 32807

This should do

/^[$]?\d{1,2}([.]\d{1,2})?$/

Upvotes: 1

Related Questions