JS验证文本框只能输入1位小数,只能输入2位小数

只能输入1位小数点:
<input  type="text" id="attendanceScore"  class="tab_bor2"  name="scoreCheck.attendanceScore"   maxlength="4"
onblur="check(this)"  
onkeyup="this.value=this.value.replace(/[^0-9.]/g,'')"  />

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

---------------------------------------------------

只能输入2位小数点:
<input id="runLength" name="runTrip.runLength" value="${runTrip.runLength}"
onkeyup="this.value=this.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3')" maxlength="6"/>

你可能感兴趣的:(文本框)