Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello
i use this code to validate my form
JavaScript
jQuery("form[class^='wpcf7-form']").validate({ 
rules:{ 
'your-name':{required:true,maxlength:50,minlength:4}, 
'your-subject':{required:true,maxlength:100,minlength:10}, 
'your-message':{required:true,maxlength:500,minlength:10}, 
'your-email':{required:true,eemail:true,maxlength:50} 
}, 
submitHandler: function(form) { 
form.submit(); 
}, 
errorPlacement: function (error, element) { 
element.css('background', '#ffdddd'); 
error.insertAfter(element); 
} 
}); 


if user enter wrong data so the background color changed to red
but if he correct the data he entered , background still red although message disappeared So how can i return back the background color to white if user fix the errors
Posted

1 solution

Use the highlight and unhighlight methods to modify the element:
JavaScript
jQuery("form[class^='wpcf7-form']").validate({
    rules:{
        ...
    },
    
    // NB: This is the default behaviour, so isn't really required:
    submitHandler: function(form) {
        form.submit();
    },
    
    // Called when the element is invalid:
    highlight: function(element) {
        $(element).css('background', '#ffdddd');
    },
    
    // Called when the element is valid:
    unhighlight: function(element) {
        $(element).css('background', '#ffffff');
    }
    
    // NB: No need for the "errorPlacement" function.
});


Alternatively, use the errorClass to specify a CSS class which will be applied to invalid elements:
JavaScript
jQuery("form[class^='wpcf7-form']").validate({
    rules:{
        ...
    },
    
    errorClass: "invalid"
    
    // No need for "errorPlacement", "highlight" or "unhighlight".
});

CSS
input.invalid, textarea.invalid, select.invalid
{
    background-color: #ffdddd;
}


.validate() | jQuery Validation Plugin[^]
 
Share this answer
 
v2
Comments
Hercal 10-Feb-15 8:12am    
Thanks Richard , worked with me with highlight/unhighlight method , but only one thing is need modification in ur code , instead of element.css('background', '#ffdddd');
it must be $(element).css('background', '#ffdddd');

Thank you so much for your support

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900