I am very new to Javascript.
I have a requirement to allow only digits and enter key as input in a textarea box
. I would also like to allow users to paste ONLY digits into the textarea
.
After searching through the questions on StackOverflow, I found the following:
function validate(key)
{
//getting key code of pressed key
var keycode = (key.which) ? key.which : key.keyCode;
var phn = document.getElementById('textarea');
//comparing pressed keycodes
if (keycode < 48 || keycode > 57)
{
return false;
}
<div>
<textarea id="textarea" rows="4" cols="50" onkeypress="return validate(event)" />
</div>
Unfortunately, this does not solve my problem completely. I want to allow users to press enter
. How can I change the above code to allow "enter" presses as well as integer paste events?