AitorAlejandro
6/19/2015 - 8:43 AM

Permitir sólo números - jQuery dependencia

Permitir sólo números - jQuery dependencia

$('.js-soloNumeros').keydown(function(event){
	// Permitir: backspace, delete, tab, escape, y enter
	if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || 
		event.keyCode == 27 || event.keyCode == 13 || 
		 // Permitir: Ctrl+A, Ctrl+C, Ctrl+V y Ctrl+X
		(event.keyCode == 65 && event.ctrlKey === true) ||
		(event.keyCode == 67 && event.ctrlKey === true) ||
		(event.keyCode == 86 && event.ctrlKey === true) ||
		(event.keyCode == 88 && event.ctrlKey === true) ||
		 // Permitir: inicio, fin, left, right
		(event.keyCode >= 35 && event.keyCode <= 39) ) {
			 // no hacer nada
			 return;
	}
	else {
		// Asegura que es un numero
		if (event.shiftKey || 
			(event.keyCode < 48 || event.keyCode > 57) && 
			(event.keyCode < 96 || event.keyCode > 105 ) ) {
			event.preventDefault(); 
		}   
	}
});