igorescobar / jQuery-Mask-Plugin

A jQuery Plugin to make masks on form fields and HTML elements.
http://igorescobar.github.io/jQuery-Mask-Plugin/
Other
4.77k stars 1.42k forks source link

How to combine to phone masks? #713

Open lumenier1 opened 5 years ago

lumenier1 commented 5 years ago

I want to pas phone numbers with different formats: '+{7}(000)000-00-00' and '8(000)000-00-00' How to do it with this mask?

pyetrosafe commented 5 years ago

Hi... Your question can be resolved by the code below (using the last version jQueryMask v1.11.1):

<div class="col-xs-6 col-sm-6 col-md-4">
  <div class="form-group">
    <label>Combined Values</label>
    <input type="text" name="combined" class="form-control">
  </div>
</div>

// Mask Phones
var combinedPhonesBehavior = function (val) {
            return val.charAt(0) == '+' ? '+{0}(000)000-00-00' : '+0(000)000-00-00';
        },
    combinedPhonesOptions = {
        onKeyPress: function(val, e, field, options) {
        field.mask(combinedPhonesBehavior.apply({}, arguments), options);
      },
      translation: { '+': { pattern: /[+]/, optional: true} }
    };

$(function() {
    $(':input[name=combinedPhones]').mask(combinedPhonesBehavior, combinedPhonesOptions);
});

See in action here: https://jsfiddle.net/pyetrosafe/ug18qj9m/5/