实例如下:
//只能输入正整数
function CheckNum(thisobj) {
if (thisobj.value == "0") {
} else {
var firstNum = thisobj.value.toString().substring(0,1);
if (thisobj.value.length==1) {
thisobj.value = thisobj.value.replace(/[^0-9]/g,'');
}else if (thisobj.value.length > 1 && firstNum == "0") {
thisobj.value = "0";
} else if (thisobj.value.length > 1) {
thisobj.value = thisobj.value.replace(/[^0-9]/g,'')
}
}
}
//只能输入金额
function ClearNoNum(obj) {
if (obj.value == "0") {
} else {
var firstNum = obj.value.toString().substring(0,1);
if (obj.value.length > 1 && firstNum == "0") {
obj.value = "0";
}
}
//先把非数字的都替换掉,除了数字和.
obj.value = obj.value.replace(/[^\d.]/g,"");
//必须保证第一个为数字而不是.
obj.value = obj.value.replace(/^./g,"");
//保证只有出现一个.而没有多个.
obj.value = obj.value.replace(/.{2,}/g,".");
//保证.只出现一次,而不能出现两次以上
obj.value = obj.value.replace(".","$#$").replace(/./g,"").replace("$#$",".");
}