簡體   English   中英

如何使用javascript限制文本框內的值,范圍為1-999

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

我希望有一個文本框,使用javascript接受介於1-999之間的數值,並且必須使用keypress事件,因為uswr不應該被允許在文本框中輸入o但是說foreg 106應該是允許的值。目前我是能夠重新打0然后它不允許用戶為后來的數字輸入0也因為它不允許輸入105我正在使用icefaces所以不能在這里使用html5而且還必須使用js並使用onkeypress事件實現,因為用戶shud不是甚至允許輸入值

代碼片段如下,我使用onkeypress =“if(event.which <49 || event.which> 57)返回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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM