1. 程式人生 > >關於js文字框數字校驗

關於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,'')" /> 轉載自:https://www.cnblogs.com/ctacy/p/6114167.html 只能輸入數字的寫法:<input  type="text" id="achievementScore" class="tab_bor2" name="scoreCheck.achievementScore"    onchange="if(/\D/.test(this.value)){alert('只能輸入數字');this.value='';}"/>