How to add validators for @ Html.TextBox () without model
This is part of my performance
@model bhavin.Models.Employee
@using (Html.BeginForm("BuynowForm", "Home"))
{
<div class="form-group">
<label>Billing Address</label>
@Html.TextBox("bill_address", null, new { @class = "form-control valid" })
</div>
<p>
<input type="submit" value="Submit" class="btn btn-primary" />
</p>
}
I want to add the required validation to it. The billing_address text field is not part of the model.employee. I am using mvc5 So how do I add a validator?
+3
source to share
2 answers
Add data-val
and data-val-required
attribute for Html.TextBox()
as shown below.
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
@using (Html.BeginForm("",""))
{
@Html.ValidationSummary()
@Html.TextBox("bill_address", null, new { @class = "form-control valid", @data_val = "true", @data_val_required = "Billing Address is required" })
<input type="submit" value="Click" id="btnSubmit" />
}
Note
-
@Html.ValidationSummary()
used to print a verification message. - Don't forget to include -
validate
andunobtrusive
JavaScript files.
+7
source to share