我在我的mvc4站点上有反馈表,我想对我的表单进行验证。
我尝试使用jQuery Validation
我添加了jQuery库和<script src="/Scripts/jquery.validate.min.js"></script>
然后我写道(一个字段可以尝试)
<script>
$(function () {
$("#feedback-form").validate();
$("#feedback-form").validate({
rules: {
Name: {
required: true,
minlength: 2
},
},
messages: {
Name: {
required: "Please enter a username",
minlength: "Your username must consist of at least 2 characters"
},
},
});
});
</script>以我的形式
@using (Html.BeginForm("Feedback", "Home", FormMethod.Post, new { id = "feedback-form" }))
{
<!-- Name -->
@Html.TextBoxFor(model => model.Name, null, new { @class = "text-field" })
<a href="#" class="link1" id="submit-button" onclick="document.getElementById('feedback-form').submit()"><em><b>Send</b></em></a>
}它不会在浏览器控制台中显示任何错误,但验证不起作用。例如,当我在字段为空的情况下按下发送按钮时,我什么也没有收到,没有消息。
怎么了?
发布于 2013-05-22 15:25:25
删除$("#feedback-form").validate();,它应该可以正常工作。
发布于 2013-05-22 15:33:58
我建议你在文档准备好的时候不要尝试验证,而是可以尝试提交表单,并且不需要在验证中添加两种验证和删除额外的,逗号的方法:
$(function () {
$("#feedback-form").on('submit', function(){
$(this).validate({
rules: {
Name: {
required: true,
minlength: 2
} // <-------------------------removed comma here
},
messages: {
Name: {
required: "Please enter a username",
minlength: "Your username must consist of at least 2 characters"
} // <-------------------------removed comma here
} // <-------------------------removed comma here
});
});
});发布于 2013-05-22 15:36:48
submitHandler: function(form) {
form.submit();
}并删除
$("#feedback-form").validate();并移除,
Name: {
} , <--- here you add "," for 1 more rule or messagehttps://stackoverflow.com/questions/16685789
复制相似问题