如何知道何时输入有一个:无效的选择器

Jquery, how to know when input have a :invalid selector?

本文关键字:无效 选择器 有一个 何知道 何时 输入      更新时间:2023-09-26

我有这样的代码:

HTML

<input type="text" data-value="1" data-type="input-records" placeholder="Value" pattern="'d{1,4}$" />
CSS

input[type=text]:invalid { background-color: red; }
Javascript

$("[data-type=input-records]").die().live("keypress", function (e) {
    if (!($(this).val().length + 1) < 5) {
        e.preventDefault();
        return;
    }
    // More code below...
});

我想做这样的验证:

if (!$(this).hasSelector(":invalid")) {
    showMessage("Invalid value");
}

使用is函数测试:invalid伪类:

if ($(this).is(":invalid")) {
    showMessage("Invalid value");
}

例子: http://jsbin.com/ikuwur/2/edit