Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I validate input fields with Twitter Bootstrap 2.0 and apply error class?

I have a field like the following:

<div class="control-group">
    <label class="control-label" for="lname">Last Name</label>
    <div class="controls">
        <input class="span3" type="text" id="lname" name="lname" placeholder="Last Name" value=""/>
    </div>
</div>

I'm trying the following jquery to validate some input and add the error class if needed:

var div = $("#lname").parent("div.control-group");
....
div.removeClass("success");
div.addClass("error");

Ideally I need to loop through several input and select fields to do my validation.

My current code isn't adding the error class. Any idea why?

like image 216
Paul Avatar asked Feb 04 '12 19:02

Paul


1 Answers

You should use parents method instead of parent as

var div = $("#lname").parents("div.control-group");
div.removeClass("success");
div.addClass("error");
like image 50
Mehdi Hadjar Avatar answered Oct 05 '22 13:10

Mehdi Hadjar