如果验证失败,请更改标签颜色

时间:2013-10-02 23:22:07

标签: javascript jquery

我有一些HTML,其中包含labelinput字段:

<div class="table-cell segmented">
    <label for="email" class="field-label">email</label>
    <input id="email" name="email" class="required" type="email" placeholder="Required"  />
    <input type="hidden" id="forgotPasswordInput" name="forgotPasswordInput" value="false"/>
</div>`

label - 验证失败时,我想用{name}的id更改input的颜色。如果验证失败,我无需更改input之后添加的标签的颜色。

我有以下方法来验证:

var validator =$("#loginForm").validate({ 
            rules: { 
                email: {
                    required:true,
                    email:true,
                    emailRule: true,
                },
                emailPass: {
                    required:true,          
                },
            }, 
            messages: { 
                email: {
                    required:"enter email",
                    email:"invalid email",
                },
                emailPass:{
                    required:"enter password",
                },
            }       
        });

2 个答案:

答案 0 :(得分:0)

我认为你需要使用invalidHandler方法。

var validator =$("#loginForm").validate({
  rules: {
    email: { required:true, email:true, emailRule: true, },
    emailPass: { required:true,}
  },
  messages: {
    email: { required:"enter email", email:"invalid email", },
    emailPass:{ required:"enter password", }
  },
  invalidHandler: function (ev, validator) {
    $('select your label').css('color', 'the new color');
  }
});

答案 1 :(得分:0)

$('.row').find('label[for=name]').css({"color": "red"});