Using a smooth animation for the jQuery Validation plugin's error messages is fairly easy to do when the message first appears. However, after the initial errorPlacement
callback is executed, I don't know how to keep animating the message as the user keeps changing the input.
In this example, I am expanding and fading the error messages if/when they first appear. After that, I want to continue to fade the error messages in and out depending on whether or not they are valid. However, only the default functionality, where the messages quickly appear/disappear (which isn't very visually appealing) is working for me.
tl;dr:
I want the error messages to always fade in and out rather than blinking.
HTML
<form id="contactForm" class="form-horizontal" autocomplete="off">
<fieldset>
<h4 style="margin-bottom: 20px; margin-top: 20px;" class="text-center">Contact us today for a free thing</h4>
<div class="form-group">
<span class="col-xs-offset-1 col-xs-1 text-center"><i class="fa fa-user bigicon" aria-hidden="true" title="Name"></i></span>
<div class="col-xs-9">
<input type="text" name="Name" placeholder="Name" class="form-control hps-text-box">
</div>
</div>
<div class="form-group">
<span class="col-xs-offset-1 col-xs-1 text-center"><i class="fa fa-envelope-o bigicon" title="Email address"></i></span>
<div class="col-xs-9">
<input type="text" name="Email" placeholder="Email address" class="form-control hps-text-box">
</div>
</div>
<div class="form-group">
<span class="col-xs-offset-1 col-xs-1 text-center"><i class="fa fa-phone-square bigicon" title="Phone number"></i></span>
<div class="col-xs-9">
<input type="text" placeholder="Phone number (eg. 123-456-7890)" class="form-control hps-text-box">
</div>
</div>
<div class="form-group">
<span class="col-xs-offset-1 col-xs-1 text-center"><i class="fa fa-pencil-square-o bigicon" title="Enter your message for us here."></i></span>
<div class="col-xs-9">
<textarea name="UserMessage" rows="7" placeholder="Enter your message for us here. We will get back to you within 2 business days." class="form-control hps-text-box"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-md-12 text-center">
<button id="submitButton" type="submit" class="btn btn-primary" autocomplete="off">Submit</button>
</div>
</div>
</fieldset>
</form>
JavaScript
$(document).ready(function() {
$("#contactForm").validate({
rules: {
Name: "required",
Email: {
required: true,
email: true
},
UserMessage: "required",
Phone: {
phoneUS: true
}
},
messages: {
Name: "Please provide your name",
Email: "Please enter a valid email address",
UserMessage: "Please enter a message"
},
errorElement: "div",
errorClass: "jqval-error",
errorPlacement: function(error, element) {
error.insertAfter(element);
error.slideDown(400);
error.animate({ opacity: 1 }, { queue: false, duration: 700 });
},
submitHandler: function(form) {
alert("Form submitted");
}
})
});
CSS
input,
select,
textarea {
max-width: none;
}
.hps-text-box {
margin-left: 10px;
}
.bigicon {
font-size: 34px !important;
}
div.jqval-error {
display: none;
opacity: 0;
margin-left: 18px;
color: #f39200;
font-weight: bold;
}