我在ASP.NET MVC中编写了一段代码,我正在尝试使用jQuery验证器的addClassRules方法。据我所知,我正在考虑
上的这些例子http://docs.jquery.com/Plugins/Validation/Reference
但是页面刚刚提交它没有显示任何错误。我想使用“addClassRules”模式进行验证,因为我正在编写动态表单。
这是我的观点。
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ValidateMyForm</title>
<script src="<%= Url.Content("~/Scripts/jquery-1.4.1.js") %>" type="text/javascript"></script>
<script src="<%= Url.Content("~/Scripts/jquery.validate.js") %>" type="text/javascript"></script>
</head>
<body>
<% using (Html.BeginForm()) { %>
<input type="text" value="" name="customer1" class="customer" />
<input type="text" value="" id="customer2" name="customer2" class="customer" />
<input type="text" name="customer3" class="customer" />
<p>
<input type="submit" name="submit" value="Submit" />
</p>
<% } %>
<script language="javascript" type="text/javascript">
jQuery(document).ready(function () {
// alias required to cRequired with new message
$.validator.addMethod("cRequired", $.validator.methods.required, "Customer name required");
// alias minlength, too
$.validator.addMethod("cMinlength", $.validator.methods.minlength, $.format("Customer name must have at least {0} characters"));
// combine them both, including the parameter for minlength
$.validator.addClassRules("customer", { cRequired: true, cMinlength: 2 });
})
</script>
</body>
</html>
有什么建议吗?
更新:功能齐全的代码......
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ValidateMyForm</title>
<script src="<%= Url.Content("~/Scripts/jquery-1.4.1.js") %>" type="text/javascript"></script>
<script src="<%= Url.Content("~/Scripts/jquery.validate.js") %>" type="text/javascript"></script>
</head>
<body>
<% using (Html.BeginForm(null, null, FormMethod.Post, new { id = "demoform" }))
{ %>
<%: Html.TextBox("myTextBox", "", new {@class = "cRequired"}) %>
<p>
<input type="submit" name="submit" value="Submit" />
</p>
<% } %>
<script language="javascript" type="text/javascript">
jQuery(document).ready(function () {
$.validator.addClassRules("cRequired", { required: true });
jQuery("#demoform").validate();
})
</script>
</body>
</html>
答案 0 :(得分:1)
之前从未使用过此验证器,但看起来您的语法有点偏差。此外,您没有在任何地方定义cRequired。
请注意,以下所有内容均未经过测试。
首先你需要定义cRequired:
$.validator.addMethod("cRequired", $.validator.methods.required,
"Customer name required");
接下来,您需要向类添加验证器(其中“YourClassName”是应该应用的类。)
jQuery.validator.addClassRules("YourClassName", {
cRequired: true
});
或强>
jQuery.validator.addClassRules({
YourClassName: {
cRequired: true
}
});
然后,您需要将类添加到输入中:
<input id="txtOne" type="text" class="YourClassName">
<input id="txtTwo" type="text" class="YourClassName">
<input id="txtThree" type="text" class="YourClassName">
希望这会有所帮助......
来源:
答案 1 :(得分:1)
事实证明你必须在jQuery(document).ready()函数中有以下行。
jQuery("#demoform").validate();
“demoform”是Form元素的名称。在我的情况下(MVC),我给它起了以下这个名字......
<% using (Html.BeginForm(null, null, FormMethod.Post, new { id = "demoform" })) { %>
答案 2 :(得分:0)
你可以尝试这个jQuery validation plugin