Is there some way using data annotations that I can check the value of a property in my model is just a single word? I know how to check it's maximum length but I've no idea how to do the single word check.
Yes you can. You can use a regex in your model for the validation.
[RegularExpression(@"\b*[a-zA-Z0-9_]\b", ErrorMessage = "Enter A Single Word Please")]
string FirstName {get; set;}
A single word of only letters and numbers with a minimum length of 4 and maximum length of 50 characters and a message showing the minimum:
[RegularExpression(@"[a-zA-Z\d]{4,}", ErrorMessage = "Invalid.")]
[StringLength(50, MinimumLength = 4, ErrorMessage = "Must be at least {2} characters long.")]
A single word of only letters with a minimum length of 4 and maximum length of 30 characters and a message showing the range:
[RegularExpression(@"[a-zA-Z]{4,}", ErrorMessage = "Invalid.")]
[StringLength(30, ErrorMessage = "Must be between {2} and {1} characters long.", MinimumLength = 4)]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.