[英]Form Validation using jQuery Validation
我在mvc4網站上有反饋表,我想對我的表單進行驗證。
我嘗試使用jQuery驗證
我添加了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>
}
它在瀏覽器控制台中沒有顯示任何錯誤,但是驗證不起作用。 例如,當我按下帶有空白字段的發送按鈕時,我什么也沒有收到,也沒有消息。
怎么了?
刪除$("#feedback-form").validate();
它應該可以正常工作。
我建議你的是不要試圖驗證文檔時准備好,而不是你可以嘗試在表格的提交,也沒有必要把驗證兩種方法,並刪除多余的,
逗號你在你的驗證有:
$(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
});
});
});
submitHandler: function(form) {
form.submit();
}
和刪除
$("#feedback-form").validate();
然后刪除
Name: {
} , <--- here you add "," for 1 more rule or message
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.