-
-
Save sioniks/4f6500c934246f086ed09c26e1d43f68 to your computer and use it in GitHub Desktop.
Javascript: input validate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* запрещаем вводить цифры | |
* http://learn.javascript.ru/keyboard-events | |
*/ | |
// keydown event | |
e = (e) ? e : window.event; | |
var charCode = (e.which) ? e.which : e.keyCode; | |
// если от 0 до 9 | |
if(charCode > 47 && charCode < 58) return false; | |
// Запрещает пользователю вводить любые символы, кроме: цифр, del, backspace, up, down, left, right. | |
// Событие: keypress | |
// *Альтернатива: можно просто использовать плагин jquery.maskedinput-1.3.min http://digitalbush.com/projects/masked-input-plugin/ | |
var validateNumericInput = function (ev) { | |
var charCode = (ev.which) ? ev.which : ev.keyCode | |
if ( | |
(charCode < 48 || charCode > 57) && | |
charCode != 8 && | |
charCode != 46 && | |
(charCode < 37 || charCode > 40) | |
) | |
{ | |
return false; | |
} | |
return true; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment