-1

I want to validate these formats in one regular expression in asp.net:

XX-XXXXXXX or XXX-XX-XXXX

These have to be numeric only no characters except the "-".

Is this possible? I've been trying without any success so I want to ask the experts.

Thanks, Pune

Puneo
  • 1
  • 2

2 Answers2

0

Try something like this:

/^([0-9]{2}-[0-9]{7}|[0-9]{2}-[0-9]{2}-[0-9]{4})$/
  • [0-9] means any character from 0 to 9.
  • {X} means X times
  • | means "or"
  • - means "-"
  • and ( and ) delimits a group for replacing
  • ^ and $ delimit the beginning and the ending of the match.
Jaumzera
  • 2,305
  • 1
  • 30
  • 44
0

The following should work given your requirements.

"(^\d{2}-\d{7}$)|(^\d{3}-\d{2}-\d{4}$)"
Craig W.
  • 17,838
  • 6
  • 49
  • 82