范围可能是例如5到10,这是我到目前为止所做的,但仍然没有达到我想要的<script>function checkpattern(elem){ var textbox = document.getElementById("textbox"); if(textbox.value.length<=10 && textbox.value.length>=5) { alert("5 to 10"); } else { if(!elem.value.match('^'+elem.getAttribute('pattern')+'$')) { alert('Accepts digits only'); } }}</script><input type = "text" name = "amt" maxlength = "10" pattern = [0-9] onchange = checkpattern(this) placeholder="Enter Amount" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter Amount '" required="">
2 回答
繁星淼淼
TA贡献1775条经验 获得超11个赞
如果您使用minlength
它,除非它的字符数超过5个,否则不会让您提交它
<input type = "text" name = "amt" minlength="5" maxlength = "10" pattern = [0-9] onchange = checkpattern(this) placeholder="Enter Amount" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter Amount '" required="">
如果您想将其设置为数量(数字),则可以使用min
和max
:
<input type = "number" name = "amt" min="5" max = "10" pattern = [0-9] onchange = checkpattern(this) placeholder="Enter Amount" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter Amount '" required="">
拉丁的传说
TA贡献1789条经验 获得超8个赞
您的代码中有很多问题,如果其他错误,并且您也没有正确传递输入值
<script>
function checkpattern(elem)
{
if(elem<=10 && elem>=5)
{
alert("5 to 10");
}
if(!elem.match(/^\d+$/))
{
alert('Accepts digits only');
}
}
</script>
<input type = "text" name = "amt" maxlength = "10" pattern = [0-9] onchange = checkpattern(this.value) placeholder="Enter Amount" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter Amount '" required="">
添加回答
举报
0/150
提交
取消