Mask - Telephone

garagecreations 15 Sep, 2015
Hello,

I want to create a telephone mask for a field, the problem is that the telephone in Brazil can have two different patterns:
'(99) 9999-9999' or '(99) 99999-9999'

I tried to implement the following Jquery:

$('#fone').mask('(00) 0000-00009');
$('#fone').blur(function(event) {
if($(this).val().length == 15){ // Celular com 9 dígitos + 2 dígitos DDD e 4 da máscara
$('#fone').mask('(00) 00000-0009');
} else {
$('#fone').mask('(00) 0000-00009');
}
});


But I always get the error: "cannot read the mask of null". Is there any easy way to solve this?

Thanks
GreyHead 16 Sep, 2015
Hi garagecreations,

you might try using jQuery in place of $ to see if that selects the element. The Mask library does support optional masks - see the docs here.

Bob
This topic is locked and no more replies can be posted.