JavaScript------表單約束驗證DOM方法

weixin_34377065發表於2017-01-13
<input id="id1" type="number" min="100" max="300" required>
<button onclick="myFunction()">驗證</button>
 
<p id="demo"></p>
 
<script>
function myFunction() {
    var inpObj = document.getElementById("id1");
    if (inpObj.checkValidity() == false) {
        document.getElementById("demo").innerHTML = inpObj.validationMessage;
    }
}
</script>

 

相關文章