Reputation: 19
I am trying to restrict alphabets in javascript. Here is the code I am using and I am able to show alert to users "Enter numbers only" if they enter alphabets.
But issue is after entering values in the textbox, if I try to backspace the values, it shows alert("Enter numbers only"
How to not show alert if I press backspace key.
Here is my code,
PasswordCheck(event)
{
var allowedRegex = /^[0-9]+$/ && event.keyCode === 8;
if (!event.key.match(allowedRegex)) {
alert("Enter Numbers only")
event.preventDefault();
}
<form (keyup)="PasscodeCheck($event)" autocomplete="off">
......
</form>
Upvotes: 0
Views: 325
Reputation: 289
PasswordCheck(event) {
var allowedRegex = /^[0-9]+$/;
if (!event.key.match(allowedRegex) && event.keyCode !== 8) {
alert("Enter Numbers only");
}
}
Upvotes: 1