I know I can use [Required], [StringLength] annotations for validating empty string and length requirements but would like to use same regular expression for validating them as well. I haven’t tried as I am not very good on regex.
Regular Expression should validate
1.Empty string (should not be allowed)
2.Length of characters (8)
3.Integer
4.Starting number (should be 1)
Here is the code:
[DisplayName("Account Number:")]
[RegularExpression("",
ErrorMessage = "An eight digit long number starting with 1 required")]
public string accountNo { get; set; }
Thanks in advance !
Use this regular expression
^1\d{7}$or
or, in a crunch,
Sample code:
–result has false value
If you don’t want to allow empty string then put following attribuite on the field.