admin管理员组

文章数量:1333210

I'm presently using jQuery inputmask for the following effect:

It's fairly simple for US & Canada phone number formatting using something like:

$("#user_phone").inputmask("mask", { "mask": "(999) 999-9999" });

I would, however, like to change this formatting context based on the "Country code" dropdown such that when it is United Kingdom, it uses another mask, etc. How would I implement this such that the mask will change context based on dropdown in js/jQuery?

Here's an example of the working source as it is now: /

UPDATE: Okay, I think I've got it working satisfactorily now and would appreciate any advice in terms of drying this up properly: /

I'm presently using jQuery inputmask for the following effect:

It's fairly simple for US & Canada phone number formatting using something like:

$("#user_phone").inputmask("mask", { "mask": "(999) 999-9999" });

I would, however, like to change this formatting context based on the "Country code" dropdown such that when it is United Kingdom, it uses another mask, etc. How would I implement this such that the mask will change context based on dropdown in js/jQuery?

Here's an example of the working source as it is now: http://jsfiddle/j9aNh/1/

UPDATE: Okay, I think I've got it working satisfactorily now and would appreciate any advice in terms of drying this up properly: http://jsfiddle/j9aNh/3/

Share Improve this question edited Apr 5, 2012 at 19:41 ylluminate asked Apr 5, 2012 at 4:12 ylluminateylluminate 12.4k17 gold badges82 silver badges161 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

I do not know if that what you are looking for but it could be as simple as that I think.

$('select').change(function() {

var country=$(this).val() //or if you want to get text you may use var country=$(this).text()
 var maskuse;
if(country=='USA')
{
maskuse="(999) 999-9999";


}
if(country=='Canada')
{

maskuse="(999) 999-9999";

}
/// and soon changing mask for a country 

$("#user_phone").inputmask("mask", { "mask": maskuse });

});

if you are looking for Selection of an input mask for a phone number, use inputmask-multi

本文标签: javascriptHow to change jQuery inputmask mask based on dropdown contextStack Overflow