我正在使用jQuery Validation插件。
如何将成功类添加到另一个元素?
如果我输入正确的电子邮件地址,我想为div.field
分配一个类 - 这是否可以使用jQuery Validation插件?
HTML:
<form id="registration" method="get">
<fieldset>
<legend>Registration Details</legend>
<div class="field">
<label for="email">Email<em>*</em>
</label>
<input type="text" name="email" id="email" class="required email" />
</div>
<div class="field">
<label for="confirm_email">Confirm email<em>*</em>
</label>
<input type="text" name="confirm_email" id="confirm_email" class="required email" />
</div>
<div class="password">
<div class="field">
<label for="password">Password<em>*</em>
</label>
<input type="password" name="password" id="password" class="required password min-length_6" />
</div>
<div class="field">
<label for="confirm_password">Confirm<em>*</em>
</label>
<input type="password" name="confirm_password" id="confirm_password" class="confirmation-of_password" title="Please enter the exact same password" />
</div>
</div>
</fieldset>
JavaScript的:
var formActions = {};
// Form initialisation
formActions.init = function () {
// Validate For Registration
$('#registration').validate({
rules: {
email: "required",
confirm_email: {
required: true,
email: true
},
password: {
required: true,
minlength: 5
},
confirm_password: {
required: true,
minlength: 5,
equalTo: "#password"
}
},
messages: {
email: {
required: "Please enter a valid email address."
},
confirm_email: {
required: "Please enter a valid email address.",
},
password: {
required: "Please provide a password",
minlength: "Your password must be at least 5 characters long"
},
confirm_password: {
required: "Please provide a password",
minlength: "Your password must be at least 5 characters long",
equalTo: "Please enter the same password as above"
}
}
});
};
$(formActions.init);
CSS:
/* Form styles
------------------------------------------*/
form {
}
legend {
font-size:1.4em;
font-weight:bold;
padding:0 10px;
}
.field {
overflow: auto;
padding: 5px 10px 10px 10px;
}
.field.terms, .field.terms label {
width:auto;
}
.field.terms input {
width:16px;
float:left;
}
label, .checkbox p, .radio p {
padding: 0.2em 0 0;
display:block;
font-size:1.2em;
}
form label {
font-size:1.2em;
margin:0 0 5px 0;
}
form label em {
color:red;
font-style:normal;
}
input, select {
width: 250px;
clear:both;
}
form label.error {
margin:0px;
font-size:1.2em;
color:Red;
}
input.checkbox {
border: none
}
input:focus {
border: 1px solid #c7e2f1;
border-top:1px solid #5794bf;
width:250px;
padding:2px;
}
input.error {
border: 1px solid #ff8282;
border-top:1px solid red;
background:#ffdcdc;
width:250px;
padding:2px;
}
select {
width: 163px;
}
.error .messages, .error .messages li {
float: left;
margin: 0;
padding: 0;
list-style: none;
}
.error .messages li {
padding: 0.1em 0 0 1.5em;
color: #b92d23;
}
.error input {
color: #b92d23;
}
.success {
background: url(/design/images/success_icon.png) 350px 0.2em no-repeat;
}
.error {
background: url(/design/images/error_icon.png) 350px 0.2em no-repeat;
}
fieldset {
border: 1px solid #ccc;
margin-bottom:10px;
}
.checkbox p, .checkbox .inputs {
float: left;
}
.checkbox p {
padding: 0;
margin: 0 0 1em;
}
.checkbox .inputs {
width: 165px;
}
.checkbox .inputs, .checkbox .inputs li {
list-style: none;
margin: 0 0 1em;
padding: 0;
}
.checkbox .inputs li {
margin: 0 0 0.3em;
}
.checkbox li label, .checkbox li input {
display: inline;
float: none;
width: auto;
}
.validate_any {
position: relative;
}
.validate_any.error {
padding-top: 2em;
background-position: 0 0.2em;
}
#terms_block {
background-position: 205px 0.2em;
}
#terms_block .messages li {
padding-top: 0.2em;
}
.validate_any .messages {
position: absolute;
left: 0;
top: 0.1em;
}
答案 0 :(得分:6)
您只需修改默认的highlight
和unhighlight
回调函数即可。您只能修改 not remove 这两个回调函数,否则添加/删除标准错误/有效类的正常行为将被破坏。
我使用了默认的回调函数来保留它们的正常行为,并添加了以下一行,它将定位父div
并应用类.newclass
。我只是为这个class
指定了一个绿色背景颜色,只是为了证明它在jsFiddle中工作。您可以根据需要重命名和重新设置此class
。
$(element).parent('div.field').addClass('newclass');
DEMO:http://jsfiddle.net/C7eWy/
将这些修改过的回调函数添加到.validate()
:
highlight: function (element, errorClass, validClass) {
if ($(element).attr("type") === "radio") {
this.findByName(element.name).addClass(errorClass).removeClass(validClass);
} else {
$(element).addClass(errorClass).removeClass(validClass);
$(element).parent('div.field').removeClass('newclass'); // <-- added this line
}
},
unhighlight: function (element, errorClass, validClass) {
if ($(element).attr("type") === "radio") {
this.findByName(element.name).removeClass(errorClass).addClass(validClass);
} else {
$(element).removeClass(errorClass).addClass(validClass);
$(element).parent('div.field').addClass('newclass'); // <-- added this line
}
},
请参阅文档以了解有关这些选项的更多信息:http://docs.jquery.com/Plugins/Validation/validate#toptions
答案 1 :(得分:2)
尝试以下方法
rules: {
email: "required",
confirm_email: {
required: true,
email: true
},
password: {
required: true,
minlength: 5
},
confirm_password: {
required: true,
minlength: 5,
equalTo: "#password"
}
},
messages: {
email: {
required: "Please enter a valid email address."
},
confirm_email: {
required: "Please enter a valid email address.",
},
password: {
required: "Please provide a password",
minlength: "Your password must be at least 5 characters long"
},
confirm_password: {
required: "Please provide a password",
minlength: "Your password must be at least 5 characters long",
equalTo: "Please enter the same password as above"
}
},
highlight:function(element, errorClass, validClass) {
$(element).parents('.control-group').addClass('error');
},
unhighlight: function(element, errorClass, validClass) {
$(element).parents('#email').removeClass('error');
$(element).parents('#email').addClass('success');
}
答案 2 :(得分:0)
if($('#emailId').valid() == 1)
$('#successDivId').addClass('success');
else
//remove class
如果验证失败,它将返回0,否则返回1.
答案 3 :(得分:0)
您还可以添加成功回调函数:
...
$('#registration').validate({
rules: {
email: "required",
confirm_email: {
required: true,
email: true
},
password: {
required: true,
minlength: 5
},
confirm_password: {
required: true,
minlength: 5,
equalTo: "#password"
}
},
success: function(){
alert("success"); // you're free to add your CSS class here
},
...