user4713247
user4713247

Reputation: 35

Don't accept the number that start with zero using jQuery validation

I'm trying to validate the input field, that shouldn't accept the numbers leading with zero like 0001, 01, 000001, 0000045, etc.

jQuery.validator.addMethod("numberNotStartWithZero", function(value, element) {
    return this.optional(element) || /^[1-9][0-9]+$/i.test(value);
}, "Please enter a valid number. (Do not start with zero)");

I found this above code and tried it. Validation is working fine. But it's not accepting single digit values (1 -9). Accepting from 10 only.

How can I fix this issue?

Upvotes: 0

Views: 1886

Answers (1)

mplungjan
mplungjan

Reputation: 178421

[1-9][0-9] is two digits

try

/^[1-9][0-9]*$/

https://regex101.com/r/5lWQa3/2

* is 0 or more of the preceding

Upvotes: 1

Related Questions