如何使用javascript将文本框中的值限制为1-999之间的值范围

How to restrict the value inside a textbox for a range of values from 1-999 using javascript

本文关键字:1-999 之间 范围 何使用 文本 javascript      更新时间:2024-01-13

我想要一个文本框,它使用javascript接受1-999之间的数字值,按键事件必须使用,因为uswr不应该被允许在文本框中输入o,但说foreg 106应该是一个允许的值。目前我可以重新限制0,然后它也不允许用户为以后的数字输入0,因为它不允许输入105我正在使用icefaces因此不能在这里使用html5,而且必须使用js和onkeypress事件来实现,因为用户甚至不允许输入值

代码片段如下所示,我在keypress="if(event.which<49|| event.which>57)return false;

试试这个方法。。。

<script type="text/javascript">
    function validateRange() {
        var txtVal = document.getElementById("<%=TextBox1.ClientID%>").value;
        if (txtVal >= 1 && txtVal <= 999) {
            return true;
        }
        else
            alert('Please enter a number between 1-999');//Also you can set this message to an existing label
            return false;
    }
</script>