Skip to content

Instantly share code, notes, and snippets.

@mir4a
Last active December 15, 2015 05:49
Show Gist options
  • Save mir4a/5211468 to your computer and use it in GitHub Desktop.
Save mir4a/5211468 to your computer and use it in GitHub Desktop.
Avoid enter any characters except numbers and plus sign
//<editor-fold desc="функция проверки введения только цифр">
var tel_01 = $('#con_tel');
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
function onlyNumbers(input) {
input.on('keypress', function(e){
// check IE version from this function <script src="https://gist.github.com/mir4a/5211477/raw/b78e8f60b2cc195a7831672cd2ee0fe308f17e3f/check_ie.js"></script>
if (checkVersion() < 9 && checkVersion() != -1) {
var char = String.fromCharCode(e.keyCode);
} else {
if (e.charCode != 0) {
var char = String.fromCharCode(e.charCode);
} else {
var char = e.keyCode;
}
}
// console.dir(e);
if (char === "+") {
return true;
} else if (char != 8 || char != 46) { // allow backspace and delete keys
return isNumber(char);
}
});
}
onlyNumbers(tel_01);
//</editor-fold>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment