Sunethpiumal
Sunethpiumal

Reputation: 641

Regular expression to validate textbox length

I want a regular expression to validate an ASP textbox field with the minimum length of 11 characters and in the middle of the string should be a "-" sign. The sample string is: "0000-011111". I want to validate the textbox to make sure user enters a minimum of 10 numbers with "-" sign after 4 digits using regular expressions. Please help me.
Thank you.

Upvotes: 0

Views: 1173

Answers (2)

stema
stema

Reputation: 93006

^\d{4}-\d{6,}$

You should use also ^ at the beginning and $ at the end to ensure that there is nothing before and after your string that you don't want to have. Also important is the {6,} so it will match at least 6 digits, without , it will match exactly 6 digits. If you want set a maximum of digits you can specify after the ,, e.g. {6,20}.

Upvotes: 2

Jens
Jens

Reputation: 25563

Use

\d{4}-\d{6}

\d represents a digit, - is a literal dash and the number in the curly brackets force the preceeding token to be present the given number of times.

Upvotes: 3

Related Questions