1、数字输入框校验
// 绑定输入校验事件 function bindCheckEvent() { $(‘input[type="text"]‘).keyup([9, 2], filterKeyboardPress); // 屏蔽当前非法按键 $(‘input[type="text"]‘).keypress([9, 2], filterKeyboardPress); // 屏蔽连续的非法按键 } // 对于数字字段,过滤键盘输入 function filterKeyboardPress(event) { // 设置默认值 var beforeDotNum, afterDotNum; if (typeof event.data == ‘undefined‘) { beforeDotNum = 9; afterDotNum = 2; } else { beforeDotNum = event.data[0]; afterDotNum = event.data[1]; } var val = $(this).val(); if (val == ‘‘) { return; } var reg1 = new RegExp(‘^\\d{‘ + (beforeDotNum + 1) + ‘}$‘); var reg2; if (afterDotNum == 0) { reg2 = new RegExp(‘^\\d{1,‘ + beforeDotNum + ‘}$‘); } else { // /^\d{1,9}\.?\d{0,2}$/ reg2 = new RegExp(‘^\\d{1,‘ + beforeDotNum + ‘}\\.?\\d{0,‘ + afterDotNum + ‘}$‘); } if (isNaN(+val) || reg1.test(val) || !reg2.test(val)) { $(this).val(val.substr(0, val.length - 1)); } }
时间: 2024-10-08 03:56:23