I just validated an input text, but when I Ctrl + a and then hit any character to get it replaced, nothing actually happens. I would like to know what is the reason for this error.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<input id="texto" type="text">
</body>
<script>
$("#texto").on("keypress", function(event){
if(event.which > 47 && event.which < 58){
return false;
}
if($(this).val().length == 5){
return false;
}
});
</script>
</html>
By having a function that limits the characters
input
in that way, what you are telling it is that any key you press when the characters are 5 does nothing.To limit the maximum size of the
input
you can do it through the html itself with the propertymaxLength
equal to the maximum number of characters allowed: