我在http://wines.effectwave.com/的侧边栏上有一个小提交表单我试图用Jquery验证它。我想检查是否有人使用默认值提交表单(例如“全名”),然后它应该引发错误。到目前为止,我已经针对空值和最小字符验证了它。但是如何检查这个默认值的条件呢?到目前为止,这是我的Jquery代码:
// JavaScript Document
$().ready(function() {
// validate signup form on keyup and submit
$("#regform").validate({
rules: {
fname: {
required: true,
minlength: 3
},
phone: {
required: true,
minlength: 8
},
comments: {
required: true,
minlength: 5
},
email: {
required: true,
email: true
},
},
messages: {
fname: {
required: "Please enter your full name",
minlength: "Your full name must consist of at least 3 characters"
},
phone: {
required: "Please enter your phone number",
minlength: "Your phone number must consist of at least 8 characters"
},
comments: {
required: "Please provide some comments",
minlength: "Your comments must be at least 5 characters long"
},
email: "Please enter a valid email address",
}
});
});
答案 0 :(得分:1)
你能考虑在HTML中使用占位符而不是值吗?
转过来:
<input type="text" name="fname" id="fname" value="Full Name"...
成:
<input type="text" name="fname" id="fname" placeholder="Full Name"...
它看起来一样,但实际的“值”是空的,所以你应该能够检查它们是否被填满。
有关占位符http://www.w3schools.com/tags/att_input_placeholder.asp
的更多信息答案 1 :(得分:0)
您必须创建自定义验证方法并将其添加到jQuery Validator。
$(document).ready(function () {
//add your custom method to validator
$.validator.addMethod("DefaultCheck", function (value, element) {
return value == "FullName" || "SomeOtherDefaultvalue"; // Default values to check against
}, 'Please add a valid fullname'); //Validation Message
$("#regform").validate({
rules: {
fname: {
required: true,
minlength: 3,
DefaultCheck: true // your custom method
}
}
});
});
答案 2 :(得分:-1)
你可以使用不相等。 J查询文档不清楚这个......
jQuery.validator.addMethod("notEqual", function(value, element, param) {
return this.optional(element) || value != $(param).val();
}, "The value can not be: " + value);
$("#regform").validate({
rules: {
fname: {
required: true,
minlength: 3,
notEqual:'Full Name'
},
phone: {
required: true,
minlength: 8
},
comments: {
required: true,
minlength: 5
},
email: {
required: true,
email: true
},
}