maicss
maicss

Reputation: 63

Why space in input checkValidity is true?

<input type="text" required>

$('input').blur(function () {
    console.log($('input')[0].checkValidity());
});

When I typed a space, the console gives a true. This is very confuse...

Upvotes: 1

Views: 636

Answers (2)

Krzysztof Atłasik
Krzysztof Atłasik

Reputation: 22635

Attibute required checks only if there is any character in input (also whitespaces).

If you want to check against blanks you could use pattern.

<input type="text" pattern="\S+" required>

Upvotes: 1

Fahmi B.
Fahmi B.

Reputation: 798

you can use the html5 pattern

i give you an example but you can put what you want

$('input').blur(function () {
    console.log($('input')[0].checkValidity());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<!-- this pattern don't allow to put a space in th input-->
<input type="text" pattern="^((?!\s).)*$" required>

Upvotes: 2

Related Questions