jQuery屏蔽输入只有第一个数字,其余为必填项 [英] jquery masked input have only first number optional rest mandatory

查看:76
本文介绍了jQuery屏蔽输入只有第一个数字,其余为必填项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jquery屏蔽的输入插件,并且需要具有以下格式的电话字段:

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

1 (222) - 000 - 1114

我的代码如下:

$("#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

,以下数字是无效

1 (222) - 000 - 11
  (222) - 00  - 0011

使用?9(999)-999- 9999" 无效,因为它使整个操作成为可选操作

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?

推荐答案

尝试一下:

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

这篇关于jQuery屏蔽输入只有第一个数字,其余为必填项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆