jQuery-Mask-Plugin icon indicating copy to clipboard operation
jQuery-Mask-Plugin copied to clipboard

How to combine to phone masks?

Open lumenier1 opened this issue 6 years ago • 1 comments

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?

lumenier1 avatar Jun 13 '19 10:06 lumenier1

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/

pyetrosafe avatar Jun 13 '19 12:06 pyetrosafe