I’m having some trouble validating a form:
-
When the validation returns error, it displays a span element
containing a cross image and a message, which is as expected. -
However, when the validation is successful, it displays multiple span elements, one next to the other, containing a checkmark image (sorry, can’t upload an image ’cause I’m a newbie here).
Code:
jQuery Validation:
$("#signupform").validate({...
errorClass: "error",
validClass: "valid",
errorElement: "span",
highlight: function(element, errorClass, validClass) {
$(element).addClass(errorClass).removeClass(validClass);
},
unhighlight: function(element, errorClass) {
$(element).removeClass(errorClass);
},
success: function(label) {
label.addClass('valid').removeClass('error');
}
});
HTML Form:
...
<div class="field">
<label for="email">Email:</label>
<input type="text" id="email" name="email" />
<span id="email_status"></span>
</div>
<div class="field">
<label for="username">Username:</label>
<input type="text" id="username" name="username" />
<span id="username_status"></span>
</div>
<div class="field">
<label for="password">Password:</label>
<input type="text" id="password" name="password" />
</div>
<div class="field">
<label for="confirmPassword">Confirm password:</label>
<input type="password" id="confirmPassword" name="confirmPassword" />
</div>
...
Note: "email_status" and "username_status" are specific for ajax availability validation.
CSS:
#signupform span{
width:auto;
height:16px;
padding-left:30px;
margin-left:10px;
display:inline-block;
vertical-align:text-bottom;
font-family: Avant Garde, Arial,sans-serif;
font-size:12px;
border:1px solid #000;
}
#signupform span.valid {
background:url("../images/tick.png") left center no-repeat;
}
#signupform span.error{
background:url("../images/error.png") left center no-repeat;
color:#B94A48;
}
What’s going wrong?
Add an check condition in the success function..
UPDATED CODE
Check Fiddle