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>
发布了26 篇原创文章 · 获赞 2 · 访问量 1420

猜你喜欢

转载自blog.csdn.net/weixin_43633797/article/details/101269511