Jquery masked input has only the first number optional rest required

Im using a jquery masked input plugin and should have a phone field in the following format:

1 (222) - 000 - 1114 

my code is as follows:

 $("#myPhone").mask("9 (999) - 999 - 9999"); 

now I can’t get it working to make the first digit optional, but the rest is mandatory. Thus, 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 as it makes all this optional

If this cannot be done in masks, can someone help me with regex to achieve this?

+7
source share
2 answers

Try the following:

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

This is not ideal, but you can use a number or a space:

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

All Articles