[英]Unable to specify custom error in jQuery validation engine
我正在擺弄這里記錄的jQuery驗證引擎
我已在本地下載了所有JS文件。 並創建了一個包含單個必需文本框字段的簡單表單。
問題1
我無法為此必需規則指定自定義錯誤(即使我已指定custom-error-message選項)。 相反,它顯示默認消息“此字段是必需的”。 我寫了這個代碼示例(所示的幫助下在這里 )的git的,但它不工作。 請指出哪里出錯了。 (我想用JS對象文字來代替使用等效的html5屬性。)
我的代碼:
<html>
<head>
<script type="text/javascript" src="scripts/jquery-1.9.0.min.js"></script>
<script type="text/javascript" src="scripts/jquery.validationEngine-en.js"></script>
<script type="text/javascript" src="scripts/jquery.validationEngine.js"> </script>
<link type="text/css" rel="stylesheet" href="styles/validationEngine.jquery.css" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
$(document).ready(function(){
$("#form1").validationEngine({'custom_error_messages' : {
'#username':{
'required':{
'message':"Hey you cannot leave this field blank."
}
}
}
});
});
</script>
</head>
<body>
<form id="form1">
<br /><br />
<h2>Form 1</h2>
Username: <input id="#username" type="text" class="validate[required]" ></input> <br />
</form>
</body>
</html>
問題2
如何指定應用於JS中哪個元素的規則,而不是在html中指定類值。 我寧願使用干凈的不顯眼的HTML,而不是在HTML中混合驗證特定的東西。 這可以通過jQuery Validation插件實現 ,如下所示:
$("#register-form").validate({ rules: { firstname: "required", lastname: "required", email: { required: true, email: true }, password: { required: true, minlength: 5 }, agree: "required" }, messages: { firstname: "Please enter your firstname", lastname: "Please enter your lastname", password: { required: "Please provide a password", minlength: "Your password must be at least 5 characters long" }, email: "Please enter a valid email address", agree: "Please accept our policy" }, submitHandler: function(form) { form.submit(); } });
這是否可以使用jQuery Validation引擎。
是的,有可能: 鏈接
例如:
<input type="email" name="email" id="email" data-validation-engine="validate[required,custom[email]]"
data-errormessage-value-missing="Email is required!"
data-errormessage-custom-error="Let me give you a hint: someone@nowhere.com"
data-errormessage="This is the fall-back error message."/>
另請注意,由於JQuery 1.9現在刪除或棄用了許多在過去版本中運行的函數,並且在幾天前添加了修復程序,因此您可能需要下載最新版本: jQuery.validationEngine v2.6.1
編輯不知道他們是否已更正,但在.js文件中替換
line28 : .live(...
通過
line 28 : .on(...
live已從jQuery 1.9中刪除
編輯:
你犯了一個小錯誤:
<input id="**#**username" type="text" class="validate[required]" ></input> <br />
#應該在那里:
<input id="username" type="text" class="validate[required]" ></input> <br/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.