简体   繁体   English

验证文本框只接受小整数

[英]Validate text box to accept only small int

如何验证文本框只接受小整数 (32767) ?

You can use the built-in features of HTML:您可以使用 HTML 的内置功能:

 <input type="number" max="32767" />

On submit, the browser will stop the submission and provide an accurate message to the user.提交时,浏览器将停止提交并向用户提供准确的消息。

you must use use regular expression.您必须使用正则表达式。

demo演示

<script>
    function clean(e) {
        var textfield = document.getElementById(e);
        var regex = /[a-z]/gi;
        textfield.value = textfield.value.replace(regex, "");

    }
</script>
<textarea id="ta" name="ta" onkeyup="clean('ta')" onkeydown="clean('ta')"></textarea>

now what will this do is that it will replace all other character such as alphabet or other symbol and it will only allow to write number and if someone will try to write alphabet or other character then it will remove them现在这样做是它将替换所有其他字符,例如字母表或其他符号,并且只允许写数字,如果有人尝试写字母或其他字符,那么它将删除它们

you should do some research of your own.你应该自己做一些研究。 this website should be the last option to get your answer该网站应该是获得答案的最后一个选项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM