实例
如果输入的值大于 100,显示一个信息:
rangeOverflow 属性
<inputid="id1"type="number"max="100"><buttononclick="myFunction()">验证</button><pid="demo"></p><script>
functionmyFunction(){ vartxt = ""; if(document.getElementById("id1").validity.rangeOverflow){ txt = "输入的值太大了"; } document.getElementById("demo").innerHTML = txt;}
</script>
如果输入的值小于 100,显示一个信息:
rangeUnderflow 属性
<inputid="id1"type="number"min="100"required><buttononclick="myFunction()">OK</button><pid="demo"></p><script>
functionmyFunction(){ vartxt = ""; varinpObj = document.getElementById("id1"); if(!isNumeric(inpObj.value)){ txt = "你输入的不是数字"; }elseif(inpObj.validity.rangeUnderflow){ txt = "输入的值太小了"; }else{ txt = "输入正确"; } document.getElementById("demo").innerHTML = txt;}// 判断输入是否为数字functionisNumeric(n){ return !isNaN(parseFloat(n)) && isFinite(n);}
</script>