使用jQuery验证的MVC Razor View不验证空字段

时间:2022-04-29 11:52:23

I have form built from a MVC model with a variety of validation requirements. If I enter the field and input an incorrect value (doesn't match regular expression or meet minimum length) the validation works just fine.

我有一个由MVC模型构建的表单,具有各种验证要求。如果我输入字段并输入不正确的值(与正则表达式不匹配或满足最小长度),则验证工作正常。

What it's not doing is validating the required fields that are left empty

它没有做的是验证空的必填字段

Here's an excerpt of my model:

这是我的模型的摘录:

public partial class Order
{
    public int Id { get; set; }
    [Required]
    [Display(Name = "Name")]
    public string ContactName { get; set; }
    [Required]
    [RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Not a valid phone number")]
    [Display(Name = "Phone")]
    public string ContactPhone { get; set; }
    [Display(Name="Email")]
    [Required(ErrorMessage = "Email is required")]
    [DataType(DataType.EmailAddress)]
    [EmailAddress]
    public string ContactEmail { get; set; }
}

When I run $("#OrderForm").validate().form() it returns true when required fields are blank.

当我运行$(“#OrderForm”)。validate()。form()时,如果必填字段为空,则返回true。

Here is the generated markup:

这是生成的标记:

<form action="/Order/SubmitOrder" id="OrderForm" method="post">
<table width="400">
    <tr>
        <th colspan="2">Contact Information</th>
    </tr>
    <tr>
        <td><label for="OrderInfo_ContactName">Name</label></td>
        <td><input class="text-box single-line" data-val="true" data-val-required="The Name field is required." id="OrderInfo_ContactName" name="OrderInfo.ContactName" type="text" value="" /></td>
    </tr>
    <tr>
        <td><label for="OrderInfo_ContactEmail">Email</label></td>
        <td><input class="text-box single-line" data-val="true" data-val-email="The Email field is not a valid e-mail address." data-val-required="Email is required" id="OrderInfo_ContactEmail" name="OrderInfo.ContactEmail" type="email" value="" /></td>
    </tr>
</table>
<input id="ordersubmit" type="submit" value="Submit" />
</form>

Here are my script tags:

这是我的脚本标签:

<script src="/Scripts/jquery-1.10.2.js"></script>
<script src="/Scripts/jquery-ui-1.10.4.custom.js"></script>
<script src="/Scripts/jquery.validate.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.js"></script>
<script src="/Scripts/order.js"></script>

What am I missing?

我错过了什么?

2 个解决方案

#1


0  

This works out of the box for me but only when I've remember to include the bundle that includes all the unobtrusive and validation scripts. In a modern ASP.NET MVC there is a bundle called ~/bundles/jqueryval which is defined as:

这对我来说是开箱即用的,但只有当我记得包含包含所有不显眼和验证脚本的包时。在现代的ASP.NET MVC中,有一个名为〜/ bundles / jqueryval的包,定义为:

bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.unobtrusive*",
                        "~/Scripts/jquery.validate*"));

Make sure you include it either in your view or in your layout.

确保将其包含在视图或布局中。

#2


0  

I was initializing the validator in document.ready with

我在document.ready中初始化验证器

$(document).ready(function () {
    $("#OrderForm").removeAttr("novalidate");
    $("#OrderForm").validate({ ignore: [] });
});

Previously, a "novalidate" attribute was being added to my form. I'm not sure why. Once I removed those 2 lines it works as expected. I'm not sure why that is, it just is.

以前,我的表单中添加了“novalidate”属性。我不知道为什么。一旦我删除了这两行,它按预期工作。我不确定为什么会这样,只是。

Thank you for input.

谢谢你的意见。

#1


0  

This works out of the box for me but only when I've remember to include the bundle that includes all the unobtrusive and validation scripts. In a modern ASP.NET MVC there is a bundle called ~/bundles/jqueryval which is defined as:

这对我来说是开箱即用的,但只有当我记得包含包含所有不显眼和验证脚本的包时。在现代的ASP.NET MVC中,有一个名为〜/ bundles / jqueryval的包,定义为:

bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.unobtrusive*",
                        "~/Scripts/jquery.validate*"));

Make sure you include it either in your view or in your layout.

确保将其包含在视图或布局中。

#2


0  

I was initializing the validator in document.ready with

我在document.ready中初始化验证器

$(document).ready(function () {
    $("#OrderForm").removeAttr("novalidate");
    $("#OrderForm").validate({ ignore: [] });
});

Previously, a "novalidate" attribute was being added to my form. I'm not sure why. Once I removed those 2 lines it works as expected. I'm not sure why that is, it just is.

以前,我的表单中添加了“novalidate”属性。我不知道为什么。一旦我删除了这两行,它按预期工作。我不确定为什么会这样,只是。

Thank you for input.

谢谢你的意见。