html input 設置只能輸入數字並設置最小值

html input 設置只能輸入數字

{# 輸入0時,也會生效 #}
<input type="text" class="form-control" style="width:75px;display: inline;" onkeyup="value=value.replace(/[^\d]/g,'')">
{# min作用只在submit時生效 #}
<input type="text" class="form-control" style="width:75px;display: inline;" onkeyup="value=value.replace(/[^\d]/g,'')" min='1'>
{# 完美解決 #}
<input type="number" style = "width:80px" placeholder="1~54" oninput="if(value > 54 || value < 1 ){showAlert('', '非法輸入', 3000);value = ''}"/>

<script>
	function showAlert(parms, msg, time) {
            if (parms){
                toastr.success(msg, 'Tips:');
                setTimeout(function () {
                toastr.clear();
                }, time)
            } else {
                toastr.error(msg, 'Tips:');
                setTimeout(function () {
                toastr.clear();
                }, time)
            }
        }
</script>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章