Html 电话号码的html输入模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32901390/
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 18:18:22  来源:igfitidea点击:

html input pattern for phone number

htmlinput

提问by Nasik Ahd

I need a patter like this [ 081 222 2224 ] with digits limit of 10 .. This is my try

我需要这样的模式 [081 222 2224] 数字限制为 10 .. 这是我的尝试

    <form action="" method="post" id="cusCreate" autocomplete="off">
      <input type="tel" name="telphone"  pattern="[0-9]{10}"  title="Ten digits code" required/>    
      <label style="font-size:9px;padding-left:20px"> Eg : 081 222 2224  </label> 
      <input type="submit" value="Submit"/>
   </form>

http://jsfiddle.net/hbmhjt0r/

http://jsfiddle.net/hbmhjt0r/

回答by

you can achieve your result with below changes in your code::

您可以通过以下代码更改来实现您的结果:

<form action="" method="post" id="cusCreate" autocomplete="off">
      <input type="tel" name="telphone" placeholder="888 888 8888" pattern="[0-9]{3} [0-9]{3} [0-9]{4}" maxlength="12"  title="Ten digits code" required/>    
      <label style="font-size:9px;padding-left:20px"> Eg : 081 222 2224  </label> 
      <input type="submit" value="Submit"/>    </form>

For your reference click here

供您参考,请单击此处

JSFiddle Demo

JSFiddle 演示

回答by x13

patern="[0-9]{3} [0-9]{3} [0-9]{4}"

patern="[0-9]{3} [0-9]{3} [0-9]{4}"

This requires the user to put in spaces like this 012 345 6789, if you want the spaces to be added automatically you should javascript in the onchange of the input. Add onchange="this.value=addSpaces(this.value);"to the input and see if this works:

这要求用户输入这样的空格012 345 6789,如果您希望自动添加空格,您应该在输入的更改中使用 javascript。添加onchange="this.value=addSpaces(this.value);"到输入中,看看这是否有效:

function addSpaces(initial){
        initial.replace("/([0-9]{3})/"," ");
        initial.replace("/[0-9]{3} ([0-9]{3})/"," ");
        return initial;
    }