views:

164

answers:

2

jQuery.validate doesn't seem to change the background color of the invalid element by default. Is it also possible to change the background color of a select element? Most of my elements are input type="text", but I need an indicator for the select form elements. I am not using the default generated messages, as they don't fit my layout.

The following code does change the background color of the input elements, but it never reverts to its previous style:

 $('form[name="register"]').validate({
        errorClass: 'jqInvalid',
        rules: {
            fnameCreate: "required", //input
            monthCreate: "required", //select
            emailCreate: { required: true, email: true }, //input
            passwordCreate: "required", //input type=password
        },
        messages: {
            fnameCreate: "",
            monthCreate: "",
            emailCreate: "",
            passwordCreate: "",
        },
        errorPlacement: function (error, element) {
            element.css('background', '#ffdddd');
        }
    });

Edit (sample)

<div class="field">
  <div class="labelName_fb">
    <label for="email">Email</label>
  </div>
  <div class="divforText3">
    <div class="divLeft"></div>
    <div class="textbox-image3">
      <input class="txt required" id="emailCreate" name="emailCreate" value="" maxlength="100" type="text"  style='width:180px'>
    </div>
    <div class="divright"></div>
  </div>
</div>

Where the input element is given the jqInvalid error class on error.

CSS

/* line 3 */ .error { background: #ffdddd; } /* I have since removed the errorClass option */
/* line 254 */ .field .txt {background:transparent none repeat scroll 0 0;border:medium none;color:#000;font-size:11px;width:130px; margin:5px 0;}

CSS screenshot in Chrome

+1  A: 

The invalid elements get a class of error by default, or in your case jqInvalid since you've set the errorClass option, just style that class like you want in the stylesheet, for example:

.jqInvalid { background: #ffdddd; }

You can override the specifics for the error messages (the default element being <label>) if you want:

label.jqInvalid { background: none; }

This CSS approach takes care of the removal...since the class is removed once it's valid. There's also a success class if you want a green background applied to valid elements, etc.

Nick Craver
I removed the errorClass and errorPlacement options, and I notice the `.error` class being added to the elements. But, my CSS doesn't override. In Firebug, the CSS is there (below the active CSS), but it's crossed out.
David
@David - There must be some other styling with the same properties that's overriding it...something with more specificity, what rule is actually having an effect, with the same property that's crossed out for your `.error` class?
Nick Craver
It's a class rule `.field .txt` where `.field` is a div wrapping around the box and `.txt` is directly applied. I thought both .txt and .jqInvalid (or error) styles would be applied if the class attribute is `class="txt jqInvalid"`??
David
@David - That's correct, both are applied. One detail most aren't aware of though, if they both have the same rules, say `background`, whichever is defined last *in the stylesheet*, that `background` wins...so you'll want `.jqInvalid` (or `.error` if you go back to defaults) defined last in most cases.
Nick Craver
I've tried it one line before and one line after the line Firebug mentions..it's overridden regardless.
David
@David - Do you have an example page? I can take a quick look...your overriding style is either declared later or more specific (which always wins).
Nick Craver
See edit for relevant HTML.
David
@David - Can you add the CSS (in the exact order it is in your stylesheet)? That's the issue here, at least that's what it sounds like.
Nick Craver
I added what I think is the relevant CSS and line numbers. Firebug mentions no other CSS other than the calculated `element.style { width: 180px; }` which appears above the `.field .txt` style
David
This was a specificity problem. `.error` and `.jqInvalid` are both less specific than `.field .txt`
David
@David - In that case, add `.field .txt.jqInvalid` to the selector so it has even greater specificity.
Nick Craver
A: 

This was a specificity problem. .error and .jqInvalid are both less specific than .field .txt

So, increasing the specificity required changing the CSS to .field input.error, which has more weight than .field .txt so the order no longer mattered.

See here for info on specificity

David