Html 如何限制用户在输入元素中输入 10 位数字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19445408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 14:34:17  来源:igfitidea点击:

How to restrict user to type 10 digit numbers in input element?

javascripthtmlcss

提问by Vivek Kumar

I want to create a input field for phone number. I am creating input field using JavaScript dynamically.

我想为电话号码创建一个输入字段。我正在使用 JavaScript 动态创建输入字段。

<input type="text" id="phone" name="phone">

How to restrict users to type only 10 digit numbers in order to get a phone number.

如何限制用户只能输入 10 位数字才能获取电话号码。

回答by Vinay Lodha

try this

尝试这个

<input type="text" name="country_code" title="Error Message" pattern="[1-9]{1}[0-9]{9}"> 

This will ensure

这将确保

  1. It is numeric
  2. It will be max of 10 chars
  3. First digit is not zero
  1. 它是数字
  2. 最多 10 个字符
  3. 第一位不为零

回答by iJade

Use maxlength

maxlength

<input type="text" maxlength="10" />

回答by Niet the Dark Absol

You can use maxlengthto limit the length. Normally for numeric input you'd use type="number", however this adds a spinny box thing to scroll through numbers, which is completely useless for phone numbers. You can, however, use the patternattribute to limit input to numbers (and require 10 numbers too, if you want):

您可以使用maxlength来限制长度。通常对于数字输入,您会使用type="number",但是这会添加一个旋转框来滚动数字,这对于电话号码完全没用。但是,您可以使用该pattern属性将输入限制为数字(如果需要,也需要 10 个数字):

<input type="text" maxlength="10" pattern="\d{10}" title="Please enter exactly 10 digits" />

回答by Vivek Kumar

Well I have successfully created my own working answer.

好吧,我已经成功地创建了自己的工作答案。

<input type="text" id="phone" name="phone" onkeypress="phoneno()" maxlength="10">

as well as

    <script>        
           function phoneno(){          
            $('#phone').keypress(function(e) {
                var a = [];
                var k = e.which;

                for (i = 48; i < 58; i++)
                    a.push(i);

                if (!(a.indexOf(k)>=0))
                    e.preventDefault();
            });
        }
       </script>

回答by Lewis

Add a maxlengthattribute to your input.

maxlength为您的输入添加一个属性。

<input type="text" id="phone" name="phone" maxlength="10">

See this working exampleon JSFiddle.

请参阅JSFiddle 上的此工作示例

回答by Pradosh

HTML

HTML

<input type="text" name="fieldName" id="fieldSelectorId">

This field only takes at max 10 digits number and do n't accept zero as the first digit.

此字段最多只需要 10 位数字,并且不接受零作为第一位数字。

JQuery

查询

jQuery(document).ready(function () {
      jQuery("#fieldSelectorId").keypress(function (e) {
         var length = jQuery(this).val().length;
       if(length > 9) {
            return false;
       } else if(e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
            return false;
       } else if((length == 0) && (e.which == 48)) {
            return false;
       }
      });
    });

回答by Vinay Pratap Singh

use a maxlength attribute to your input.

对您的输入使用 maxlength 属性。

<input type="text" id="phone" name="phone" maxlength="10">

See the fiddle demo here Demo

在此处查看小提琴演示Demo

回答by Scary Wombat

whereas you could use maxLengthon the input, and some javascript validation, it will still be necessary to do server side validationanyway.

虽然您可以在输入上使用maxLength和一些javascript 验证,但无论如何仍然需要进行服务器端验证

回答by Nilesh

HTML

HTML

<input type="text" id="youridher" size="10">

Use JQuery,

使用JQuery

SIZE = 10
$(document).ready(function() {
      $("#youridher").bind('keyup', function() { 
            if($("#youridher").val().length <= SIZE && PHONE_REGEX) {
                return true;
            }
            else {
                return false;
            }  
      });
});

回答by Sonali Samantaray

How to set a textbox format as 8 digit number(00000019)

如何将文本框格式设置为 8 位数字 (00000019)

string i = TextBox1.Text;
string Key = i.ToString().PadLeft(8, '0');
Response.Write(Key);