Sunday 15 June 2014

Javascript regex for number check and maximum length of 3 and does not start with 0 -



Javascript regex for number check and maximum length of 3 and does not start with 0 -

currently trying find regular expression, following

1) checks number

2) allows max length of 3

3) not start 0

i have next regex works fine

^[0-9]{1,3}$

above regular look allows numbers have maximum 3 digits not sure how prevent 0 in beginning.

code

var numericregex = new regexp("^[1-9][0-9]{0,2}$"); numericregex.test(123) //false

let me know missing

thanks

try this:

^[1-9][0-9]?[0-9]?$

or:

^[1-9][0-9]{0,2}$

in node.js can used this:

> "123".match(/^[1-9][0-9]{0,2}$/) [ '123', index: 0, input: '123' ] > "023".match(/^[1-9][0-9]{0,2}$/) null > "123".match(/^[1-9][0-9]?[0-9]?$/) [ '123', index: 0, input: '123' ] > "023".match(/^[1-9][0-9]?[0-9]?$/) null

and other reply fine:

> "123".match(/^(?!0)\d{1,3}$/) [ '123', index: 0, input: '123' ] > "023".match(/^(?!0)\d{1,3}$/) null

javascript regex

No comments:

Post a Comment