I am validating a <input text>
and I want that no more than 5 characters are written and that they do not accept numbers, but I do not get results.
$("#texto").on("keypress", function(event){
if(event.which > 47 || event.which < 58 || $(this).val().length == 5){
return false;
}
});
<!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>
</html>
Update
The problem can be solved with just html without the help of jquery as follows:
To prevent numbers from being entered, the attribute can be used
onkeypress
:and to limit the maximum number of characters that can be entered into the input you can add:
For more information see:
Block numbers, letters and/or special characters in an input
previous answer
The code is fine, you only have a logic problem in this part:
When you put
event.which > 47 || event.which < 58
this it will always be true, therefore no character will be written. The correct form is:since you want to avoid numbers between 0 and 9 .
The working code is as follows:
I hope it helps you, greetings.