日期:2014-05-16  浏览次数:20382 次

js数字输入的验证~

?

?

?

$("input[id^='d_1_f']").blur(function(){

var length = new Number(this.getAttribute('maxlength')-5);

check(2,parseInt(length),4,this);

});

?

//验证数字的输入

function check(pattern,num1,num2,obj){

var numReg = null;

var flag = true;

if(pattern == 1){

numReg = new RegExp("\^\\d{1," + num1 + "}\$");

if(obj.value.length != 0 && !numReg.test(obj.value)){

alert("您输入的数据不正确,只能输入整数!");

obj.value = "";

obj.focus();

flag = false;

}

} else if (pattern == 2) {

numReg = new RegExp("\^\\d{1," + num1 + "}(\\.{1}\\d{1,"+ num2 +"})?\$");

if(obj.value.length != 0 && !numReg.test(obj.value)){

alert("您输入的数据不正确,最大允许" + num1 + "位整数," + num2 + "位小数");

obj.value = "";

obj.focus();

flag = false;

}

} else?

flag = false;

return flag;

}