user875690
user875690

Reputation: 227

Masked Input Plugin

I need to do an implementation in the "Masked Input Plugin" to make it work masks accepting the following values:

(99) 9999-9999 or (99) 99999-9999

I tried this:

$('.tel').mask('(99) 9999? 9-9999');

but if I report the value (99) 9999 9999 as it renders (99) 99999-999 I do not want

Upvotes: 2

Views: 520

Answers (1)

Ragen Dazs
Ragen Dazs

Reputation: 2170

You can use this nice solution

$('.tel').focusout(function(){
    var phone, element;
    element = $(this);
    element.unmask();
    phone = element.val().replace(/\D/g, '');
    if(phone.length > 10) {
        element.mask("(99) 99999-999?9");
    } else {
        element.mask("(99) 9999-9999?9");
    }
}).trigger('focusout');

Original code found at

http://pedroelsner.com/2012/07/mascara-jquery-para-novo-digito-de-celular-em-sao-paulo/

Upvotes: 1

Related Questions