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

Time:03-05

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?

CodePudding user response:

[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

  • Related