I want to ignore certain characters in my phone input, so that the database just has digits. I know I can do this easy on server side (using PHP) but I am trying to understand js events a little better.. My question is this:
If I have a basic input:
var phoneInput = document.getElementById("phoneInput");
I can add an event listener using "onkeydown" which works fine
phoneInput.onkeydown = function(e){
var c = String.fromCharCode(e.keyCode);
var patt = /\d/;
if(!patt.test(c)) return false;
};
But if I try doing the same thing using 'addEventListener', returning false seems to do nothing
phoneInput.addEventListener("keydown",function(e){
var c = String.fromCharCode(e.keyCode);
var patt = /\d/;
if(!patt.test(c)) return false;
});
I just don't understand why. Thanks in advance for any light you can shine on the subject..
The keydown and keyup events provide a code indicating which key is pressed, while keypress indicates which character was entered. For example, a lowercase "a" will be reported as 65 by keydown and keyup , but as 97 by keypress .
The keydown event occurs when a keyboard key is pressed down. The keydown() method triggers the keydown event, or attaches a function to run when a keydown event occurs.
To record a keydown event in JavaScript, use the code below: // Add event listener on keydown document. addEventListener('keydown', (event) => { var name = event. key; var code = event.
keydown – on pressing the key (auto-repeats if the key is pressed for long), keyup – on releasing the key.
I would strongly advise against changing the user's input or otherwise prevent them from typing something while they're doing it. It is confusing and leads to a bad user experience.
Ideally, you should keep your server-side validation and then use HTML5 features such as these:
<input type="number" /> Allows only numbers
<input type="text" pattern="[0-9. -]*" /> Allows numbers, spaces, periods and hyphens
<input type="text" required /> Specifies a required field
Modern browsers will prevent the form from being submitted and present helpful error message to the user (which you can customise with a title
attribute).
However, for general reference, return false;
doesn't necessarily cancel the event. To do that, you should use this:
// if you haven't already:
e = e || window.event;
// to cancel the event:
if( e.preventDefault) e.preventDefault();
return false;
I had to do something similar for a project I'm working on. This is how I did it.
// prevent users from typing alpha/ symbol characters on select fields
$("#modal-region").on("keydown", "#markdown, #sku", function(e) {
var key = e.which;
// when a keydown event occurs on the 0-9 keys the value
// of the "which" property is between 48 - 57
// therefore anything with a value greater than 57 is NOT a numeric key
if ( key > 57) {
e.preventDefault();
} else if (key < 48) {
// we don't want to disable left arrow (37), right arrow (39), delete (8) or tab (9)
// otherwise the use cannot correct their entry or tab into the next field!
if (key != 8 && key != 9 && key != 37 && key != 39 ) {
e.preventDefault();
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With