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
html input pattern for phone number
提问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>
回答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>
回答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;
}