I have created one view ,in which i have one text box,i want to put required field validation on that text box.
@Html.TextBox("txtFirst", "", htmlAttributes: new {@maxlength="9"})
I have created one view ,in which i have one text box,i want to put required field validation on that text box.
@Html.TextBox("txtFirst", "", htmlAttributes: new {@maxlength="9"})
First I must say the best way to do validation in MVC is to put the Data Annotation attributes above the properties in your model like with this:
[Required]
[StringLength(9)]
public string Foo {get; set;}
// This will force the validation in the client side.
@Html.TextBoxFor(m => m.Foo);
The good thing in this approach(except that it's usually less to write) that it work client-side and Server-side as well.
If you want to do the validation in the view and not in the Model for some reason, you simply need to add the required class to the textbox:
@Html.TextBox("txtFirst", "", htmlAttributes: new {@class = "required", maxlength="9"})