Link to home
Start Free TrialLog in
Avatar of DanielAttard
DanielAttardFlag for Canada

asked on

Captures paste even in javascript and validate input

I have some javascript code that runs on keyup and when the user has entered 15 consecutive digits, that is when the important code is triggered.  The code below works find when a user types the digits consecutively:

$('#rollNum').keyup(function(e) {
if(e.keyCode == 8) { //on backspace 
	// do stuff
} else if ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105)) { //0-9 only
	// do stuff
	if(textLen < 15) {
		// do stuff
	} else	if(textLen == 15){
		// do important stuff

Open in new window


My problem happens when a user pastes a 15-digit string directly into the input box instead of typing the numbers on at a time.  When the user pastes 15-digits, this does not trigger the important code that runs on the 15th digit.

Can anyone suggest the best way to validate user input at the moment when there is a paste event into an input?  Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DanielAttard

ASKER

Thanks for the help.  Much appreciated.