最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin21PV0评论

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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far