How can I annotate my model so I can allow only alphabets like A-Z in my text-box?
I know that I can use regex but can anyone show how to do that on text-box property itself using data annotation.
Add a KeyPress event handler on the TextBox. Then put in this code into the textbox. This will allow only alphabets or /.
You could annotate your model like this:
[RegularExpression(@"^[a-zA-Z]+$", ErrorMessage = "Use letters only please")]
string TextBoxData {get; set;}
Then in your view you would use the helper
@Html.EditorFor(model => model.TextBoxData)
@Html.ValidationMessageFor(model => model.TextBoxData )
You can use annotations for regular expression validation (if i understood your questions), something like that
[RegularExpression("[a-zA-Z]",ErrorMessage="only alphabet")]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With