0

I need to use an asp:RegularExpressionValidator control in my asp.net site and I want to restrict the characters to digits and semi-colon only.

So it needs to be able to take any amount of digits and ;

Valid values:

123

123;456

123;456;789;....

What is the regex for that?

2
  • If you use ^\d+(?:;\d+)*;?$? input like ";;5" will be invalid (caught by ^[\d;]+$). Commented May 6, 2015 at 14:14
  • same if you try with ^((?:[0-9]+[;])+)$ Commented May 6, 2015 at 15:01

1 Answer 1

3

Try this simple regex: [0-9;]+ or [\d;]+

  • \d match a digit [0-9]
  • ; the literal character ;

  • + means that it match between one character and unlimited times, as many times as possible,

If you want to guarantee that at least numbers are present in your expression you could do this too:

@npinti has a valid point better will be: ^[\d;]+$

where ^ indicates the begging of your expression and $ the end of it.

Online Demo

Sign up to request clarification or add additional context in comments.

3 Comments

The above would match abc9 or abc;. Add the ^ and $ and you should be good to go.
Always use [0-9] instead of \d with the RegularExpressionValidator. Although client-side validation works properly with \d, server-side validation incorrectly accepts digits from other Unicode scripts as well, like (Bengali 9).
Please note that this will validate ;;5. See my comment above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.