只允许输入数字和小数点

只要给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;
}

}
}
}
})();

posted @ 2015-09-06 00:37  向着太阳生  阅读(517)  评论(0编辑  收藏  举报