views:

53

answers:

1

I would like to either/both change the label font color to RED on error or shade the textbox element red. The CSS I'm using is not working for me and not sure how to change the label of the radio/textbox instead if the label error which is added by the validate plugin.

// HTML

<form id="form1" name="form1" class="form1" method="post" >     
    <label for="gender">
        Gender                          
    </label>

    <input type="radio" name="gender" value="female" id="gender_0" />
        Female
    </input>
    <br />

    <input type="radio" name="gender" value="male" id="gender_1" />
        Male
    </input>
        <br />

        <label for="name">Name</label>                    
        <input type="text" name="name" id="name" />
    <br /><br />

    <input type="submit" name="Submit" value="Submit" />
</form>

// JS

$().ready(function() {

    $("#form1").validate({
        rules: {
            gender: "required",
                        name: "required"
        },
        messages: {
            gender: "Please select an option",
                        name: "Please enter your name"
        }
    });
});

// CSS

#form1 label.error {
    margin-left: 10px;
    width: auto;
    display: inline;
}
+1  A: 

You could wrap each label - input combo in a div and then use the built-in highlight function:

HTML

<form id="form1" name="form1" class="form1" method="post" >
    <div>    
        <label for="gender">Gender</label>
        <br /><input type="radio" name="gender" value="female" id="gender_0" />Female
        <br /><input type="radio" name="gender" value="male" id="gender_1" />Male
    </div>
    <div>
        <label for="name">Name</label>                    
        <input type="text" name="name" id="name" />
    </div>

    <br /><br />
    <input type="submit" name="Submit" value="Submit" />
</form>

jQuery

$("#form1").validate({
    rules: {
        gender: "required",
        name: "required"
    },
    messages: {
        gender: " - Please select gender",
        name: " - Please enter your name"
    },
    errorElement: "span",
    errorPlacement: function(error, element) {
        element.siblings("label").append(error);
    },
    highlight: function(element) {
        $(element).siblings("label").addClass("error");
    },
    unhighlight: function(element) {
        $(element).siblings("label").removeClass("error");
    }
});

CSS - the error/highlight class

.error {
    color: red;
}

JSFiddle Demo here.

Hope this helps !

FreekOne
hmm not working, in firebug says 'element.siblings("label").addClass("error");' not a function
Phill Pafford
Ooopsie, my bad. I have updated the highlight/unhighlight part in the jQuery script.
FreekOne
this just jumbles the elements out of place, nothing is displayed
Phill Pafford
Indeed it does, my apologies. I have updated the code again, and I have also added a working JSFiddle demo.
FreekOne
Thanks works awesome!!! just one more question. How do I get a alert popup after submit is successful?
Phill Pafford
Well, unless you submit the form over AJAX, I wouldn't recommend having an alert dialog upon submission because that will lock the browser and the form won't actually be submitted until the user clicks on `OK`. Instead, I think that you would be better off if you print out a "thank you" message on the next page. Nevertheless, if you look at my JSFiddle demo, I used [`submitHandler`](http://docs.jquery.com/Plugins/Validation/validate#code) to replace the default behavior and show an alert instead of submitting the form.
FreekOne