Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This should work fine.
^000[0-9]{6}$

^ 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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading