使用: Jquery和jquery验证js
错误:如果文本框值和plceholder字符串相同,则会触发所需的字段验证,
代码:
<form method="post" action="#">
<input type="text" name="name" placeholder="Your">
<input type="text" name="email" placeholder="you@example.com">
<button type="submit">Submit</button>
</form>
// Test if the submitted value is equal to our placeholder.
$.validator.addMethod('notPlaceholder', function(val, el) {
return ( val !== $(el).attr('placeholder') );
}, $.validator.messages.required);
$('form').validate({
rules: {
name: {
required: true,
notPlaceholder: true
},
email: {
required: true,
notPlaceholder: true,
email: true
}
},
// This is just for jsfiddle.
debug: true,
submitHandler: function(form) {
$.each( $(form).serializeArray(), function(i, f) {
$(form).append('<p>' +f.value+ '</p>');
});
}
});
必填:表单应发布
任何人都可以帮助我吗?
答案 0 :(得分:1)
只需删除此fiddle
中的notPlaceholder: true
即可
//rest of your code here
rules: {
name: {
required: true,
//notPlaceholder: true // <- REMOVE ME
},
email: {
required: true,
//notPlaceholder: true // <- REMOVE ME
email: true
}
},
//rest of your code here
答案 1 :(得分:1)
$('form').validate({
rules: {
name: {
required: true,
},
email: {
required: true,
email: true
}
},
// This is just for jsfiddle.
debug: true,
submitHandler: function(form) {
$.each( $(form).serializeArray(), function(i, f) {
$(form).append('<p>' +f.value+ '</p>');
});
}
});
你的jquery应该是
答案 2 :(得分:0)
试用此代码:
$.validator.addMethod('notPlaceholder', function(val, el) {
return ( val !== $(el).attr('placeholder') );
}, $.validator.messages.required);
$('form').validate({
rules: {
name: {
required: true,
},
email: {
required: true,
email: true
}
},
// This is just for jsfiddle.
debug: true,
submitHandler: function(form) {
$.each( $(form).serializeArray(), function(i, f) {
$(form).append('<p>' +f.value+ '</p>');
});
}
});
答案 3 :(得分:0)
我不知道你到底在做什么,但你可以这样做:
$.validator.addMethod('notPlaceholder', function(val, el) {
return !(val !== $(el).attr('placeholder'));
}
这应该有效。
要提交表格,您可以:
$(form)[0].submit();