Phone mask with jQuery and Masked Input Plugin

前端 未结 15 1452
夕颜
夕颜 2020-11-28 06:54

I have a problem masking a phone input with jQuery and Masked Input Plugin.

There are 2 possible formats:

  1. (XX)XXXX-XXXX
  2. (
15条回答
  •  一整个雨季
    2020-11-28 07:38

    If you don't want to show your mask as placeholder you should use jQuery Mask Plugin.

    The cleanest way:

    var options =  {
        onKeyPress: function(phone, e, field, options) {
            var masks = ['(00) 0000-00000', '(00) 00000-0000'];
            var mask = (phone.length>14) ? masks[1] : masks[0];
            $('.phone-input').mask(mask, options);
        }
    };
    
    $('.phone-input').mask('(00) 0000-00000', options);
    

提交回复
热议问题