[英]jQuery Validation — Not validating
我正在構建我的第一個ASP.net MVC應用程序(而不是我的第一個jQuery和jQuery Validation插件應用程序),並且用驗證插件執行客戶端驗證的時間很糟。 有誰知道VS 2008附帶的jQuery-1.3.2.min插件是否存在問題?
我的代碼如下:
var v = $("#frmAccount").validate({
rules: {
txtAccount: { minLength: 5,required: true },
txtFName: "required",
txtLName: "required"
},
message: {
txtAccount: {
required: "Account Number is required.",
minLength: jQuery.format("Account Number must be {0} or more charaters.")
},
txtFName: "First Name is required.",
txtLName: "Last Name is required.",
}
}); //validate
$("#cmd").click(function() {
if (v.form()) {
$("#frmAccount").submit();
}
});
.....................................
<form method =“ post” action =“ / Home / checkAccount” id =“ frmAccount”>
<fieldset>
<legend>帳戶信息</ legend>
<p>
<br />
<label for =“ txtAccount”>帳號:</ label>
<br />
<input id =“ txtAccount” name =“ txtAccount” type =“ text” value =“” />
<br />
<label for =“ txtFName”>名字:</ label>
<br />
<input id =“ txtFName” name =“ txtFName” type =“ text” value =“” />
<br />
<label for =“ txtLName”>姓氏:</ label>
<br />
<input id =“ txtLName” name =“ txtLName” type =“ text” value =“” />
<br />
<label for =“ txtLName”>電子郵件:</ label>
<br />
<input id =“ txtEmail” name =“ txtEmail” type =“ text” value =“” />
</ p>
<p>
<input type =“ button” id =“ cmd” name =“ cmd” value =“添加” />
</ p>
</ fieldset>
</ form>
當我嘗試提交空白表格時,它將在account,fname和&lname字段旁邊返回默認的“此字段為必填字段”,但是如果我在字段中輸入任何數據,則會得到各種結果。
我試圖只執行基本的validate函數(不將validate對象返回給v變量),但是產生了相似的結果。
我只是在這里忽略了一些愚蠢的事情嗎?
Adrian Grigore,感謝您的快速回復。 但是,我認為您可能忽略了上面發布的代碼或其他內容。 也許您太忙於插入自己的博客之類的東西了 。 IDK。
REAL問題如下:
*在上面的代碼中,“ message”應為“ message s ”
*此外,我犯了(我認為)基本的MS編程背景錯誤...我駱駝了工作minLength,並且(當然)在jQuery中應該是minlength
Adrian,祝您博客愉快!
jquery.validate確實可以在jquery 1.32上正常工作。 您所說的“各種結果”是什么意思?
無論如何,由於您使用的是ASP.NET MVC,因此建議您不要手動編碼客戶端驗證。 如果使用xVal和DataAnnoationModelBinder代替它,則更容易,更穩定並且可以完全測試。 查看有關使用ASP.NET MVC進行客戶端驗證的文章。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.