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

Ser*_*res 10 javascript validation jquery

我有这个代码:

HTML

<input type="text" data-value="1" data-type="input-records" placeholder="Value" pattern="\d{1,4}$" />
Run Code Online (Sandbox Code Playgroud)

CSS

input[type=text]:invalid { background-color: red; }
Run Code Online (Sandbox Code Playgroud)

使用Javascript

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

    // More code below...
});
Run Code Online (Sandbox Code Playgroud)

我想做这样的验证:

if (!$(this).hasSelector(":invalid")) {
    showMessage("Invalid value");
}
Run Code Online (Sandbox Code Playgroud)

And*_*ker 20

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

if ($(this).is(":invalid")) {
    showMessage("Invalid value");
}
Run Code Online (Sandbox Code Playgroud)

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