如何使用带有jquery的asp.net mvc验证错误消息?

问题描述 投票:0回答:1

我的表单具有以下字段:街道地址,街道地址第2行,城市,州/省,邮政编码和国家/地区。离开表单时,我正在努力验证错误消息。

我已尝试在模型上使用Required(错误消息=“必填字段”)。我没有使它起作用。所有上述提到的字段必须用红色突出显示,但是离开州/省后,必须立即通过错误消息“必填字段”进行验证。这是我到目前为止的代码尝试做的尝试。

// Model class
[Required(ErrorMessage = "This field is required")]
public string State { get; set; }

View.cshtml

<div class="row">
   <label for="Address">Address</label>
   <div class="col-md-6 ">
      <div class="input-group pull-right">
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
         @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">
         &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
         @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">
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            @Html.TextBoxFor(m => m.HomeMainModel.City, new { @class = "form-control", type = "text", id = "inputFormVal", autofocus = "autofocus", placeholder = "City" })&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            @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">
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            @Html.TextBoxFor(m => m.HomeMainModel.Code, new
            {
            @class = "form-control",
            type = "text",
            id = "inputFormVal",
            autofocus = "autofocus",
            placeholder = "Postal/Zip Code"
            })
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            @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";
    }
  });
});
javascript c# asp.net-mvc bootstrap-4
1个回答
0
投票

将其添加到您的底部页面,并检查您是否具有验证器脚本。

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
© www.soinside.com 2019 - 2024. All rights reserved.