15

私はこのコードを持っています:

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

1 に答える 1

27

関数を使用して、疑似クラスisをテストします。:invalid

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

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

于 2013-01-17T17:24:25.593 に答える