I have a single textbox in a form that I would like to add some validation to.
How can I add some unobtrusive validation attributes to it without using a model?
For eg.
@using (Html.BeginForm())
{
@Html.ValidationSummary()
@Html.Label("code", "Confirmation Code")
@Html.TextBox("code")<!-- I want validation on this thing -->
<input type="submit" value="Go" />
}
How can I make it required
, and others such as stringlength, etc.
HtmlHelpers will render HTML elements that contain a data-*
attribute that contain all the data that the unobtrusive JavaScript uses to perform the client validation.
I'm not aware of any resources documenting the data-*
HTML attributes so I would suggest creating a simple temporary model decorated with the required Data Annotations and viewing the rendered HTML.
Once you know the HTML attributes required then you can render them directly in your HTML.
Update
This Brad Wilson post may help
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