jquery masked input have only first number optional rest mandatory

北城以北 提交于 2019-12-22 04:58:13

问题


Im using jquery masked input plugin and need to have a phone field with the following format:

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

If this cant be done in masked can someone help me out with regular expression to achieve this?


回答1:


Try this:

$.mask.definitions['~'] = '([0-9] )?';
$("#myPhone").mask("~(999) - 999 - 9999");



回答2:


It is not perfect, but you can use number or whitespace:

$.mask.definitions['~'] = '[0-9 ]';
$("#myPhone").mask("~(999) - 999 - 9999");


来源:https://stackoverflow.com/questions/15371025/jquery-masked-input-have-only-first-number-optional-rest-mandatory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!