$(document).ready(function(){
$("input:text").each(function(){
// 改变前的值
var oldValue = $(this).val();
// keyup 事件后若仍匹配则设置keyup后的值
$(this).bind("keyup", function(){
if(checkInput(this)){
oldValue = $(this).val();
}
});
// change 事件
$(this).bind("change", function(){
// change 后值不匹配设为旧值
if(!checkInput(this)){
$(this).val(oldValue);
} else {
oldValue = $(this).val();
}
});
});
});
/*判断输入值,若 alt={i,j} 则匹配成小数点后有i~j位,若alt为空或不存在则为整数*/
function checkInput(obj){
var str = obj.value;
var alt = obj.alt;
var exp = "^\\d+$";
if(""!==alt && null !== alt && undefined != alt){
exp = "^\\d+(\\.\\d"+alt+")?$";
}
var regExp = new RegExp(exp);
return regExp.test(str);
}