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

时间:2013-01-17 17:22:08

标签: javascript jquery validation

我有这段代码:

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");
}

1 个答案:

答案 0 :(得分:20)

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

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

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