使用jQuery验证的MVC Razor View不验证空字段
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.
确保将其包含在视图或布局中。
更多相关文章
- 如何在java脚本中获取节点内部文本?
- 如何从Python脚本向jQuery发送JSON对象?
- 使用jQuery在AJAX调用中的url字段格式
- 可编辑的表格:jQuery+PHP实现实时编辑表格字段内容
- 为什么我不能在同一个js文件中建立两个cleave.js格式的字段?
- 在使用javascript将信息输入到输入字段时填充文本区域
- 更改html隐藏字段的事件
- Symfony 2在用户站点上动态添加字段以形成
- Ajax调用php脚本返回404错误