ASP.NET MVC validation : string starts with 000 and is followed by another 6 numbers

I am validating a string that should be 9 chars long, numbers only and must start with 000.

I created the following validation:

[RegularExpression("^[0]{3}*", ErrorMessage="{0} must start with 000 and be numeric")]
[StringLength(9, MinimumLength=9, ErrorMessage = "{0} must be 9 numb long")]
public string Test{get;set;}

Is there better way to do it?

>Solution :

This should work fine.

^ asserts position at start of a line
000 matches the characters 000 literally (case sensitive)
Match a single character present in the list below [0-9]
    {6} matches the previous token exactly 6 times
    0-9 matches a single character in the range between 0 and 9 (case sensitive)
$ asserts position at the end of a line

Test it here

Leave a Reply