在实际项目的开发过程中,常常需要从键盘只输入数字或者数字和一些特定的字符,对于其他的字符禁止输入,这里本文将项目常用的几种案例做一列举,如:telephoneOrFax是对电话号码和传真的校验;ext是对分机号的校验;price是对价格的校验。希望对大家有一定帮助,第一次写博客还望大家多多指点。
function telphoneOrFax(item) {
numberValidate(item, /[^0-9(\-)]/g);
}
function ext(item) {
numberValidate(item, /[^0-9]/g);
}
function price(item) {
numberValidate(item, /[^0-9]./g);
}
function numberValidate(item, regular) {
item.keyup(function () {
$(this).val($(this).val().replace(regular, ''));
}).bind("paste", function () {
$(this).val($(this).val().replace(regular, ''));
}).css("ime-mode", "disabled");
}