I have a model with a property defined like this:
[Required(ErrorMessage="Please enter how many Stream Entries are displayed per page.")] [Range(0,250, ErrorMessage="Please enter a number between 0 and 250.")] [Column] public int StreamEntriesPerPage { get; set; }
This works unless the user enters something like "100q". Then a rather ugly error is displayed that says "The value '100q' is not valid for StreamEntriesPerPage."
Is there an attribute I can use to override the default error message when input is not an int?
The data-valmsg-for 's value is the name (not the id ) of the input it refers to, and data-valmsg-replace="true" just means that the default message should be replaced, for example you could have a default message for the email field: <span class="field-validation-valid" data-valmsg-for="email" data-valmsg-replace=" ...
ModelState. IsValid indicates if it was possible to bind the incoming values from the request to the model correctly and whether any explicitly specified validation rules were broken during the model binding process.
Yes, you can use Data annotations extensions, mark your property as the following:
[Required(ErrorMessage = "Please enter how many Stream Entries are displayed per page.")] [Range(0, 250, ErrorMessage = "Please enter a number between 0 and 250.")] [Column] [DataAnnotationsExtensions.Integer(ErrorMessage = "Please enter a valid number.")] public int StreamEntriesPerPage { get; set; }
Try adding
[RegularExpression("\\d+", ErrorMessage = "some message here")]
Reference blog post
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