Hi Team
I want to validate when leaving textbox, error message of 5 textbox. Each of these except one textbox field must have error label red. Meaning i have street name, street address line2, city, post code, country, state/province. As soon you leave textbox(State/Province) It must validate error message "Field is required". I have tried using model Required data notation i could not success, please view my logic below.
- // Model class
- [Required(ErrorMessage = "This field is required")]
- public string State { get; set; }
- ```
- View.cshtml
- ```html
- class="row">
- ="Address">Address
class="col-md-6 ">class="input-group pull-right">- @Html.TextBoxFor(m => m.HomeMainModel.Address, new { @class = "form-control", type = "text", id = "inputFormVal",autofocus = "autofocus", placeholder = "Street Address", required = "required" })
class="input-group-append">class="input-group-text">class="col-md-6">- class="text-danger" style="display:none">
- class="row">class="col-md-6 ">class="input-group pull-right">
- @Html.TextBoxFor(m => m.HomeMainModel.Address, new { @class = "form-control", type = "text", id="inputFormVal", autofocus = "autofocus", placeholder = "Street Address Line 2", required = "required" })
class="input-group-append">class="input-group-text">class="col-md-6">- class="text-danger" style="display:none">
- class="form-group">class="input-group mb-2">class="input-group-prepend">class="input-group col-md-7 col-md-offset-7 col-sm-7 col-xs-7">class="input-group pull-right">
- @Html.TextBoxFor(m => m.HomeMainModel.City, new { @class = "form-control", type = "text", id = "inputFormVal", autofocus = "autofocus", placeholder = "City" })
- @Html.TextBoxFor(m => m.HomeMainModel.Code, new { @class = "form-control", type = "text", id = "inputFormVal", autofocus = "autofocus", placeholder = "Province" })
class="col-md-6">- class="text-danger" style="display:none">
class="input-group mb-2">class="input-group-append">class="input-group col-md-7 col-md-offset-3 col-sm-2 col-xs-2">class="input-group pull-right">- @Html.TextBoxFor(m => m.HomeMainModel.Code, new
- {
- @class = "form-control",
- type = "text",
- id = "inputFormVal",
- autofocus = "autofocus",
- placeholder = "Postal/Zip Code"
- })
- @Html.DropDownListFor(m => m.HomeMainModel.SelectedCountryId, this.ViewBag.CountryList as SelectList, new { @class = "form-control" })
class="input-group-append">class="input-group-text">class="col-md-6">- class="text-danger" style="display:none">
- ```
- ```javascript
- // function when leaving texbox for city, street address, street_address_line2, state_province.
- $(function() {
- $('#inputFormVal').blur(function() {
- var city = document.getElementById("inputFormVal").value;
- var expr = /^([\w-\.]+)@@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
- if (!expr.test(city)) {
- document.getElementById("labelMessageBx").style.display = "inline";
- } else {
- document.getElementById("labelMessageBx").style.display = "none";
- }
- });
- });
- ```
Rajanikant HawaldarPosted Apr 9, 2020, 2:47 AM
Piyush PansuriyaPosted Apr 9, 2020, 2:39 AM