Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery.maskedinput programmatically set value and apply mask?

I'm using DigitalBush's MaskedInput jquery plugin. I have a simple input box for a phone number:

$('#txt_PhoneNumber').mask('(999) 999-9999');

Sometimes the field is programmatically filled with a non-formated number such as 5551234567. How can I notify the mask input to apply it's mask to "beautify" it's new input?

like image 479
Corey Ogburn Avatar asked Dec 18 '13 17:12

Corey Ogburn


2 Answers

Perhaps things have changed since this question was asked, because Jason P's first suggestion appears to work now.

$('#txt_PhoneNumber').trigger('input');

jsfiddle

The following should work too:

$('#txt_PhoneNumber').trigger('paste');
like image 83
John S Avatar answered Nov 11 '22 00:11

John S


I was able to get it to refresh by calling .trigger("focus") : JSFiddle

The downside: it highlights the input.

like image 27
solskido Avatar answered Nov 11 '22 00:11

solskido