Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manually add validation to textbox without a model

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.

like image 637
Shawn Mclean Avatar asked Oct 11 '22 12:10

Shawn Mclean


1 Answers

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

like image 102
David Glenn Avatar answered Nov 03 '22 01:11

David Glenn