This is a part of my view
@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 required validation to it. The billing_address textbox is not a part of the models.employee. I am using mvc5 So how to add the validator?
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()
is used for printing validation message.validate
and unobtrusive
JavaScript files.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