Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Validation: How to make fields red

I am using jquery and jquery.validate.cs (jQuery Validation Plugin 1.8.0) to validate a form. Right now it displays a message next to a field saying : "This is a required field".

I also want each field to turn red color. How would i do that? Thanks!

like image 347
sam Avatar asked May 29 '11 16:05

sam


People also ask

How to use validate function in jQuery?

Then to define rules use simple syntax. jQuery(document). ready(function() { jQuery("#forms). validate({ rules: { firstname: 'required', lastname: 'required', u_email: { required: true, email: true,//add an email rule that will ensure the value entered is valid email id.

What is remote validation in jQuery?

According to the jQuery validate documentation for remote: The response is evaluated as JSON and must be true for valid elements, and can be any false, undefined or null for invalid elements, using the default message; or a string, eg. "That name is already taken, try peter123 instead" to display as the error message.


2 Answers

Without any custom configuration, you can just do this:

select.error, textarea.error, input.error {
    color:#FF0000;
}
  • The default class that is applied to an invalid input is error
  • The default class for an input that was validated is valid.

These classes are also applied to the error label, so be aware of that when writing your CSS.

  • Demo with default classes: http://jsfiddle.net/wesley_murch/j3ddP/2/

The validation plugin allows you to configure these class names, so you may do something like this:

$("form").validate({
   errorClass: "my-error-class",
   validClass: "my-valid-class"
});

.my-error-class {
    color:#FF0000;  /* red */
}
.my-valid-class {
    color:#00CC00; /* green */
}
  • Demo with custom classes: http://jsfiddle.net/wesley_murch/j3ddP/1/

The configuration options can be found at http://docs.jquery.com/Plugins/Validation/validate

like image 50
Wesley Murch Avatar answered Sep 18 '22 18:09

Wesley Murch


$("#myform").validate({
   error: function(label) {
     $(this).addClass("error");
   },
});

use the errorClass parameter to add the .invalid class:

input.error {
    color: red;
}
like image 29
Sujit Agarwal Avatar answered Sep 19 '22 18:09

Sujit Agarwal