tommychoo
tommychoo

Reputation: 653

How to use regex to limit 0 - 100 and NOT blank input in Javascript?

I have a problem about regex expression in Angular/Javascirpt.

I have a regex expression as below. This is work for limit 0-100.

But how to limit the input is not blank ? For example user type something text in the input textfield, then user press the delete or backspace key to empty the textfield, the regex work !

ng-pattern="/^[0-9][0-9]?$|^100$/"

I already put the regex in this.

Upvotes: 0

Views: 873

Answers (1)

Niitaku
Niitaku

Reputation: 835

You can use the following pattern:

^(?:[1-9]?\d|100)$

It will allow 100 and other number below, but not numbers starting with 0such as 01. Blank is also not matched but 0 alone is.

See the regex demo

Upvotes: 1

Related Questions