English 中文(简体)
JQuery validation plugin - error highlight problem
原标题:

I have a form with two input textboxes, and I have included jQuery validation rules for both:

<script src="../../Scripts/jquery-validate/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function() {
    $( #respondForm ).validate({ onclick: false,
      onkeyup: false,
      onfocusout: false,
      highlight: function(element, errorClass) {
        $(element).css({ backgroundColor:  Red  });
      },
      errorLabelContainer: $("ul", $( div.error-container )),
      wrapper:  li ,
      rules: {
         text : {
          required: true,
          minlength: 5,
          maxlength: 10
        },
        integer: {
          required: true,
          range: [0, 90]
        }
      },
      messages: {
         text : {
          required: "xxx_Required",
          minlength: "XXX Should be greater than 5",
          maxlength: "XXX Cannot be greater than 10"
        },
        integer: {
          required: "is required",
          range:  "is out of range: [0,90]"
        }
      }
    });
  });
</script>
</head>
.
.
.
<input type="text" id="text" name="text" />    
<br />
<input type="text" id="integer" name="integer" />
<br />
<input type="submit" name="submit" value="Submit" />
<br />

I have used:

function(element, errorClass) {
  $(element).css({ backgroundColor:  Red  });
}

to highlight the error control. Now the problem is that in the following scenario, both the input textboxes remain highlighted (background color: red):

  1. Input text with less than 5 characters in text box 1
  2. Leave text box 2 blank
  3. Hit submit
  4. Both input text box background will be changed to red (which is correct)
  5. Now enter a text which has 6 characters in text box 1 (valid input)
  6. Leave text box 2 empty
  7. Hit submit
  8. The background color for both the textboxes remains red. Where as the expectation is that the background color of text box 1 should not be red

How do I resolve this problem?

最佳回答

Found the answer, you have to provide an unhighlight property as well.

Adds the error class to both the invalid element and its label

$(".selector").validate({
  highlight: function(element, errorClass) {
     $(element).addClass(errorClass);
     $(element.form).find("label[for=" + element.id + "]")
                    .addClass(errorClass);
  },
  unhighlight: function(element, errorClass) {
     $(element).removeClass(errorClass);
     $(element.form).find("label[for=" + element.id + "]")
                    .removeClass(errorClass);
  }
});

More info

问题回答

The function you used to highlight the error control sets the css property backgroundColor to red using the jQuery.css() function, which puts the rule in the style attribute on the element. If you don t have another callback function that resets the background on the element to inherit using the jQuery.css() function, or otherwise overrides/removes the rule in the style tag, then the rule will stay in place and the form element will continue to have a red background.

What you should really do is set the background on the form element indirectly by applying a css class rule to it (Is the errorClass argument to your callback supposed to be a css classname to apply on error? I that case you should use that). That way when the form submits you could easily reset the appearance of the form and revalidate:

$( #theForm .errorClassName ).removeClass( errorClassName );
$( #theForm ).validate();




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

Drop down background url in Safari Issue

selectBox.selectCSS { background: url(/Images/replacementSelectBackground.png) top left no-repeat height:auto; } I have an issue in Safari only where the image is not rendering on top ...

CSS specific for Safari

How do you target specifically safari in css with styles?

Aligning textarea in a form

Ive used the following css code to align my form elements: form { position:relative; } form input { position:absolute; left:11em; } However, the textarea element is not aligned correctly with the ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

CSS problem with page footer

I have defined my page footer in the css file as: #footer { position: absolute; height: 50px; text-align: center; background: #66CCCC; bottom: 0px; left: 0px; width: 100%; height: ...

热门标签