I want to add a required field validation on textboxes, I’m not using any model parameters(Annotations) for these textboxes. How to achieve this?
Following is my code:
@using (Html.BeginForm())
{
<h1> 3D Pay Örnek Sayfa</h1>
<table class="tableClass">
<tr class="trHeader">
<td>Test</td>
<td>@Html.TextBox("TestIt")</td>
</tr>
</table>
}
Well, you really should use a model, but if you insist doing things by hand, I’d still recommend using the jquery validation plugin.
An example based on http://docs.jquery.com/Plugins/Validation#Example:
(Note: the bit you’re looking for is
@Html.TextBox("TestIt", "", new { @class="required" }), which add the class=”required” attribute to the textbox, which in turn tells jquery validate that it’s a required field.)Please remember this is just client side validation – you also need to validate on the server side too. As you’re not using a model, this will probably mean some custom controller code to validate each input.