jQuery Validate的占位符问题 [英] placeholder issue with jQuery Validate
问题描述
我今天注意到,与占位符文本一起使用时,jquery Validate存在问题.
I noticed today that we have an issue with jquery Validate when used in conjunction with placeholder text.
示例:
<div class="sort left">
<label for="username" class="inlineelement">Username</label>
<input id="registername" type="text" placeholder="your name" autocomplete="off" class="required"></input>
我注意到,如果我们使用jquery Validate( http://docs.jquery .com/Plugins/Validation/Methods )
I noticed that , if we validate our form using jquery Validate ( http://docs.jquery.com/Plugins/Validation/Methods )
是否可以解决此问题,还是应该返回到表单字段中以html(占位符文本)为焦点?
Is there a work around for this, or should be go back to focus html ( placeholder text ) within our form fields ?
推荐答案
我遇到了同一问题.经过一番摸索后,找到了使用此自定义方法的解决方法.此自定义方法将容纳占位符.
I was coming across the same issue. After some messing around, found a workaround with this custom method. This custom method will accommodate placeholders.
jQuery.validator.addMethod("placeholder", function(value, element) {
return value!=$(element).attr("placeholder");
}, jQuery.validator.messages.required);
在additional-methods.js
文件末尾标记此行.
然后您将按以下方式使用它:
You'll then use it as follows:
$("form").validate({
rules: {
username: {required: true, placeholder: true},
},
message: {
username: {
required: "Username required", placeholder: "Username required",
},
}
});
这篇关于jQuery Validate的占位符问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!