【问题标题】:I don't want to allow a 9 digit number in textbox我不想在文本框中允许 9 位数字
【发布时间】:2016-09-16 10:44:09
【问题描述】:

我不想在文本框中允许 9 位数字。文本框应该接受字母,任何数字都接受 9 位数字。并且文本框不应该除了 9 位数字,即使它的格式如下:123-23-2323

【问题讨论】:

  • 我们帮助那些自助的人。至少发布您的尝试。
  • 那么,一个 8 位数字,即使有破折号(即 82-22-4444)也可以,但 9 位数字无效?
  • 到目前为止你尝试了什么?
  • 也许this是你需要的
  • 尝试最大长度属性并设置为 9

标签: javascript jquery asp.net asp.net-mvc asp.net-mvc-4


【解决方案1】:

试试这个:

HTML:

<input type="text" id="txtName" onkeypress="return Allow9NumberOnly(this,event)" />

Jquery:

function Allow9NumberOnly(item, evt) {
        evt = (evt) ? evt : window.event;
        var charCode = (evt.which) ? evt.which : evt.keyCode;

        if (charCode >= 48 && charCode <= 57) {
            var myText = $(item).val();
            var intCount = 0;
            for (var i = 0; i < myText.length; i++) {

                if (!isNaN(myText[i])) {
                    intCount++;
                }
            }

            if (intCount >= 8) {               
                return false;
            }
        }

        return true;       
    }   
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="txtName" onkeypress="return Allow9NumberOnly(this,event)" />

=============================================

如果您只想限制字符 9,那么:

function Allow9NumberOnly(item, evt) {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode == 57)
    {
        return false;
    }     

    return true;       
}

【讨论】:

    猜你喜欢
    • 2017-11-22
    • 2021-02-07
    • 2013-01-22
    • 1970-01-01
    • 1970-01-01
    • 2016-06-01
    • 1970-01-01
    • 2010-10-25
    • 1970-01-01
    相关资源
    最近更新 更多