1

Before creating this function when something was incorrect in inputs HTML5 was triggering default invalid action for browser (which was setting border color of input into red etc). But now, if entered value is invalid, it doesn't do anything on blur event. Can I trigger browser's default invalid action for input when if's condition not met?

(function ($) {
    $.fn.isValid = function () {
        return document.getElementById(this[0].id).checkValidity();
    };
})(jQuery);
$(function () {
    $(".answer").blur(function () {
        if ($(this).isValid()) {
          //do something
        }
        else
         //trigger default invalid action
    })
});
demonoid
  • 318
  • 3
  • 13
  • 40

1 Answers1

0

Javascript/jQuery isn't necessary. You can utilize the :invalid CSS pseudo-class; this lets you apply a specific style to invalid elements. Similarly, valid elements match the :valid pseudo-class.

Source: https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms/Data_form_validation

sbonami
  • 1,892
  • 17
  • 33
  • As I said, it was triggering when there was no JS like I provided. Now my JS prevents triggering invalid state – demonoid Oct 22 '15 at 19:30
  • "You can't force a certain pseudo-class to apply using jQuery" http://stackoverflow.com/a/12741013/1106878 – sbonami Oct 22 '15 at 20:22
  • Curious that watching blur stops default, that shouldn't be happening. You don't call `event.preventDefault` or return false, do you? – sbonami Oct 22 '15 at 20:34