0

I need to match a number of 1-5 digits or empty string for model validation:
Model:

[RegularExpression(@"\d{0,5}", ErrorMessage = "Error")]
public string ServiceNumber { get; set; }

View:

@Html.TextBoxFor(m => m.ServiceNumber)

This doesn't let me leave out the input empty.

2
  • So you're getting an error on empty string? Commented Apr 24, 2012 at 1:02
  • @Jack, yes Html.ValidationSummary displays the message. Commented Apr 24, 2012 at 1:12

2 Answers 2

1

Have you tried \d{1,5}|^$? The ^$ is an empty string because ^ is the start of the string and $ is the end of the string in regex so ^$ means start and end string with nothing inside. The | means OR, either match 1-5 digits OR empty string.

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

1 Comment

\d{0,5} can never fail to match.
1

Have you tried @"(\d{1,5})?" ?

Comments

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.