0

I want to validate my model to accept only 10 numbers and the number should not start with 700. Any idea how can I do it using data annotation?

[Required(ErrorMessage = "This field is required ")]
[StringLength(10, ErrorMessage = " CR should be 10 digits")]
public string CR_Number { get; set; } 
3

1 Answer 1

1
 [Required(ErrorMessage = "This field is required ")]
 [StringLength(10, ErrorMessage = " CR should be 10 digits")]
 [RegularExpression("^(?!700)\d", ErrorMessage = "CR should not start with 700.")]
 public string CR_Number { get; set; } 

Or you can merge the last two as suggested in the comments "^(?!700)\d{10}$".

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

2 Comments

Can you please tell me how can I make it starts with 700 and accept only 10 digits?
^(700)([0-9]{7})$

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.