js 验证文本框只能输入数字和小数点

第一步、添加js方法 

function check(e) { 
    var re = /^\d+(?=\.{0,1}\d+$|$)/ 
    if (e.value != "") { 
        if (!re.test(e.value)) { 
            alert("请输入正确的数字"); 
            e.value = ""; 
            e.focus(); 
        } 
    } 



第二步、调用【加粗的都要有】 
<input  type="text" id="achievementScore" class="tab_bor2" name="scoreCheck.achievementScore"  
onblur="check(this)"  onkeyup="this.value=this.value.replace(/[^0-9.]/g,'')" />

 

 
posted @ 2016-11-29 16:35  沈超的博客园  阅读(28987)  评论(0编辑  收藏  举报