标签:
只要给input元素加上number-point这个类就可以自动验证
<input type="text" class="number-point"/>
(function(){
var classList = document.getElementsByClassName("number-point");
for(var i=0;i<classList.length;i++){
classList[i].onkeyup = function () {
var value = this.value;
if(value.search(/^\d+\.?\d$/) != 0){
value= value.replace(/[^.0-9]/g,‘‘);
var index = value.indexOf(‘.‘);
var index2 = value.indexOf(‘.‘,index+1);
if(Number(index)>0 && Number(index2)>0){
this.value = value.substring(0,index2);
}else{
this.value = value;
}
}
}
}
})();
标签:
原文地址:http://www.cnblogs.com/toward-the-sun/p/4784297.html