html代码
<input type="text" onkeyup="clearNoNum(this)">
js代码
function clearNoNum(obj) {
var str,num,arr,len,bool;
obj.style.imeMode =‘disabled‘ //禁止输入法
obj.value = obj.value.replace("-", "$#$").replace(/-/g, "").replace("$#$","-");
obj.value = obj.value.replace(".", "$#$").replace(/./g, "").replace("$#$",".");
arr = obj.value.substring(0,1);
num = /^[0-9]*$/.test(arr);
len = obj.value.substring("0",1);
if( ((arr == "-") || (num == true && arr !== "0" )) || /^[0-9]*$/.test(obj.value) && len !== "0"){
bool = obj.value.slice(1).indexOf("-");
if(/^[0-9]*$/.test(bool)){
obj.value = null
}
obj.value = obj.value.replace(/^(-)*(d+).(dd).*$/, ‘$1$2.$3‘);// 只能输入两个小数
obj.value = obj.value.replace(/[^-?d.]/g, ""); // 清除"数字"和"."和"-"以外的字符
if(obj.value !== "-" && obj.value !== "" & obj.value !== null){
var val = parseFloat(obj.value);
if (val >= 100000000000000000) {
obj.value = "99999999999999.99";
}
console.log(obj.value) //最终值
}
}else{
obj.value = null;
}
}
写的不好!
希望大神们指点
错误或者可以简写的地方