Link to home
Start Free TrialLog in
Avatar of brgdotnet
brgdotnetFlag for United States of America

asked on

Regular expression for field in the format of NNNN-NNN-NNNN

I have an Asp.net web form with an input field where people will enter the data in the format
 NNNNN-NNN-NNNNN

Where N represents a number. And each set of numbers is seperated by a hyphen. Also two consecutive hyphens are not allowed. Also I need to prevent the applicaiton user from starting the number sequence with a hyphen or ending with a hyphen. So this is not allowed:
-NNNNN-NNN-NNNNN Nor -NNNNN-NNN-NNNNN-  Nor -NNNN-NNN-NNNN-

Basically the rule is a first set of numbers followed by a hyphen, followed by another set of numbers, a hyphen and followed by third set of numbers. The total number of digits entered cannot exceed 20.

Can someone help me figure this out?  I initially created this regular expression which I used to allow people to enter up to 20 numbers. The requirements have changed now so that I have to use hypens for input according to what I described earlier. This is what I initially had:
^\d{1,20}$

This is a difficult problem I have not been able to figure out. I will offer 400 points for some expert help.
Avatar of Superdave
Superdave
Flag of United States of America image

^(?=.{1,20}$)\d+-\d+-\d+$
SOLUTION
Avatar of drypz
drypz
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
^\d{1,20}-\d{1,20}-\d{1,20}$
Sorry, didn't see TOTAL digits count should be less than 20.
1) ^\d+-\d+-\d+$
2) And set MaxLength for TextBox equal to 18
That's it
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial