最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery, how to know when input have a :invalid selector? - Stack Overflow

programmeradmin4浏览0评论

I have this code:

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

I want to make a validation like this:

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

I have this code:

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

I want to make a validation like this:

if (!$(this).hasSelector(":invalid")) {
    showMessage("Invalid value");
}
Share Improve this question asked Jan 17, 2013 at 17:22 Sergio FloresSergio Flores 5,4275 gold badges38 silver badges60 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 28

Use the is function to test for the :invalid pseudoclass:

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

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论