jQuery 验证插件 - 显示自定义消息
我正在努力将 jQuery 验证插件用于表单...请参阅下面的完整代码。虽然下面的代码检查项目名称是否为必需,但它不检查长度,并允许提交。看出什么问题了吗?
<form method="post" id="new_space" data-remote="true" class="new_space" action="/spaces" accept-charset="UTF-8">
<label for="space_name">Name</label><br>
<input type="text" size="30" name="space[name]" id="space_name" class="text_field">
</form>
<script type="text/javascript">
$(document).ready(function () {
$("#new_space").validate({
errorLabelContainer: "#ui-dialog-errors",
wrapper: "p",
errorClass: "error",
invalidHandler: function() {
$("#ui-dialog-errors").hide().fadeIn();
},
rules: {
"space[name]":{required: true, minLength: 4}
},
messages: {
"space[name]":{ required: "Project Name required!", minLength: "Project Name's need 4+ characters" }
}
});
});
</script>
I'm working to use the jQuery validation plugin for a form... see full code below. While the below is checking for the project name to be required, it's not checking length, and allows the submit. See anything wrong?
<form method="post" id="new_space" data-remote="true" class="new_space" action="/spaces" accept-charset="UTF-8">
<label for="space_name">Name</label><br>
<input type="text" size="30" name="space[name]" id="space_name" class="text_field">
</form>
<script type="text/javascript">
$(document).ready(function () {
$("#new_space").validate({
errorLabelContainer: "#ui-dialog-errors",
wrapper: "p",
errorClass: "error",
invalidHandler: function() {
$("#ui-dialog-errors").hide().fadeIn();
},
rules: {
"space[name]":{required: true, minLength: 4}
},
messages: {
"space[name]":{ required: "Project Name required!", minLength: "Project Name's need 4+ characters" }
}
});
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
minLength
更改为minlength
(使用小写“l”)应该可以解决此问题。对于你的第二个问题,假设
form_submit
是你的提交按钮的id
,这应该可以解决问题:在 JSFiddle。
Changing
minLength
tominlength
(with lowercase "l") should fix it.For your second question, assuming
form_submit
is theid
of your submit button, this should do the trick:See it on JSFiddle.