У меня есть форма, которая имеет следующие поля: адрес улицы, строка адреса улицы 2, город, штат / провинция, почтовый индекс и страна. Я изо всех сил пытаюсь проверить сообщение об ошибке при выходе из формы.
Я пытался использовать Обязательный (Сообщение об ошибке = "Поле обязательно для заполнения") на модели. Я не смог заставить это работать. Все эти упомянутые поля должны быть выделены красным, но как только вы покидаете штат / провинцию, они должны подтвердить это с сообщением об ошибке «Поле обязательно для заполнения». Вот моя попытка того, что код пытается сделать до сих пор.
// Model class
[Required(ErrorMessage = "This field is required")]
public string State { get; set; }
View.cs html
<div class="row">
<label for="Address">Address</label>
<div class="col-md-6 ">
<div 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" })
<div class="input-group-append">
<div class="input-group-text">
</div>
</div>
</div>
</div>
<div class="col-md-6">
<label id="labelMessageBx" class="text-danger" style="display:none"></label>
</div>
</div>
<hr />
<div class="row">
<div class="col-md-6 ">
<div 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" })
<div class="input-group-append">
<div class="input-group-text">
</div>
</div>
</div>
</div>
<div class="col-md-6">
<label id="labelMessageBx" class="text-danger" style="display:none"></label>
</div>
</div>
<hr />
<div class="form-group">
<div class="input-group mb-2">
<div class="input-group-prepend">
</div>
<div class="input-group col-md-7 col-md-offset-7 col-sm-7 col-xs-7">
<div 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" })
</div>
</div>
</div>
<div class="col-md-6">
<label id="labelMessageBx" class="text-danger" style="display:none"></label>
</div>
<hr />
<!--Zip code for postal code-->
<div class="input-group mb-2">
<div class="input-group-append">
</div>
<div class="input-group col-md-7 col-md-offset-3 col-sm-2 col-xs-2">
<div 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" })
<div class="input-group-append">
<div class="input-group-text">
</div>
</div>
</div>
</div>
<div class="col-md-6">
<label id="labelMessageBx" class="text-danger" style="display:none"></label>
</div>
</div>
</div>
// 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";
}
});
});