jquery 屏蔽输入只有第一个数字可选其余强制性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15371025/
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
jquery masked input have only first number optional rest mandatory
提问by GGio
Im using jquery masked input plugin and need to have a phone field with the following format:
我正在使用 jquery 屏蔽输入插件,并且需要具有以下格式的电话字段:
1 (222) - 000 - 1114
my code looks like this:
我的代码是这样的:
$("#myPhone").mask("9 (999) - 999 - 9999");
now i cant seem to get it to work to make the first digit optional but the rest mandatory. So the following numbers are valid:
现在我似乎无法让它工作以使第一个数字可选,但其余数字是强制性的。所以以下数字是有效的:
1 (222) - 000 - 1114
(222) - 000 - 1114
and the following numbers are invalid
以下数字无效
1 (222) - 000 - 11
(222) - 00 - 0011
using "?9 (999) - 999- 9999"will not work since it makes the whole thing optional
使用"?9 (999) - 999- 9999"将不起作用,因为它使整个事情变得可选
If this cant be done in masked can someone help me out with regular expression to achieve this?
如果这不能在蒙版中完成,有人可以用正则表达式帮助我实现这一目标吗?
回答by vcampitelli
Try this:
尝试这个:
$.mask.definitions['~'] = '([0-9] )?';
$("#myPhone").mask("~(999) - 999 - 9999");
回答by Sertage
It is not perfect, but you can use number or whitespace:
它并不完美,但您可以使用数字或空格:
$.mask.definitions['~'] = '[0-9 ]';
$("#myPhone").mask("~(999) - 999 - 9999");