Johnson
Johnson

Reputation: 113

Simple integer regular expression

I have ValidationRegularExpression="[0-9]" which only allows a single character. How do I make it allow between (and including) 1 and 7 digits? I tried [0-9]{1-7} but it didn't work.

Upvotes: 11

Views: 13254

Answers (2)

stema
stema

Reputation: 92986

If you want to avoid leading zeros, you can use this:

^(?!0\d)\d{1,7}$

The first part is a negative lookahead assertion, that checks if there is a 0 followed by a number in the string. If so no match.

Check online here: http://regexr.com?2thtr

Upvotes: 3

Heinzi
Heinzi

Reputation: 172270

You got the syntax almost correct: [0-9]{1,7}.

You can make your solution a bit more elegant (and culture-sensitive) by replacing [0-9] with the generic character group "decimal digit": \d (remember that other languages might use different characters for digits than 0-9).

And here's the documentation for future reference:

Upvotes: 9

Related Questions