对ASP.Net MVC验证进行故障排除?电子邮件验证电话不起作用

时间:2018-09-17 17:46:10

标签: jquery asp.net-mvc unobtrusive-validation

我有一个页面正在加载以下库:

<script src="/Scripts/jquery-3.3.1.js"></script>
<script src="/Scripts/jquery-migrate-3.0.0.js"></script>
<script src="/Scripts/jquery-ui-1.12.1.js"></script>
<script src="/Scripts/jquery.validate.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.js"></script>
<script src="/Scripts/expressive.annotations.validate.js"></script>

该表单有几个需要验证的字段。

  <div class="row">
    <div class="small-12 large-4 column end">
        <label class="label radius" for="PrimaryPhone">Home Phone</label>
        <input class="text-box single-line" id="PrimaryPhone" name="PrimaryPhone" placeholder="(###) ###-####" type="tel" value="" />
        <span class="field-validation-valid" data-valmsg-for="PrimaryPhone" data-valmsg-replace="true"></span>
    </div>
</div>
<div class="row">
    <div class="small-12 large-4 column en">
        <label class="label radius" for="OtherPhone">Other Phone</label>
        <input class="text-box single-line" id="OtherPhone" name="OtherPhone" placeholder="(###) ###-####" type="tel" value="" />
        <span class="field-validation-valid" data-valmsg-for="OtherPhone" data-valmsg-replace="true"></span>
    </div>
</div>
<div class="row">
    <div class="small-12 large-4 column end">
        <label class="label radius" for="WorkPhone">Work Phone</label>
        <input class="text-box single-line" id="WorkPhone" name="WorkPhone" placeholder="(###) ###-####" type="tel" value="" />
        <span class="field-validation-valid" data-valmsg-for="WorkPhone" data-valmsg-replace="true"></span>
    </div>
</div>
<div class="row">
    <div class="medium-6 small-12 end column">
        <label class="label radius" for="EmailAddress">Email Address</label>
        <input class=" text-box single-line" id="EmailAddress" name="EmailAddress" type="email" value="" />
        <span class="field-validation-valid text-danger" data-valmsg-for="EmailAddress" data-valmsg-replace="true"></span>
    </div>
</div>

验证基于ViewModel

    [Display(Name = "Email Address")]
    [DataType(DataType.EmailAddress)]
    [EmailAddress(ErrorMessage ="Invalid Email Address")]
    [RegularExpression(@"([\w\.\-_]+)?\w+@[\w-_]+(\.\w+){1,}", ErrorMessage = "Invalid Email Address")]
    public string EmailAddress { get; set; } [Display(Name = "Other Phone")]

    [DataType(DataType.PhoneNumber)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:###-###-####}")]
    [RequiredIf("PrimaryPhone==null && WorkPhone==null", ErrorMessage ="You must enter at least one phone number.")]
    [RegularExpression(@"(?!(?:1-)?(\\$|#|800|833|844|855|866|877|888|900|0))\(?[\\s.-]*([0-9]{3})?[\\s.-]*\)?[\\s.-]*[0-9]{3}[\\s.-]*[0-9]{4}", ErrorMessage = "Please enter a valid phone number")]
    public string OtherPhone { get; set; }

电子邮件地址可以正确验证,但电话不能正确验证。此外,远程验证属性也已停止工作。

****添加了生成以上代码的剃须刀代码片段

    @Html.LabelFor(model => model.PrimaryPhone, htmlAttributes: new { @class = "label radius" })
        @Html.EditorFor(model => model.PrimaryPhone, new { htmlAttributes = new { placeholder = "(###) ###-####" } })
        @Html.ValidationMessageFor(model => model.PrimaryPhone)

1 个答案:

答案 0 :(得分:0)

部分安装/卸载Expressive Annotations导致删除了默认验证提供程序,而没有添加替代项。不知道为什么会发生部分安装。我刚刚添加了适当的提供程序,一切按预期进行。