Error masking Javascript

Asked

Viewed 361 times

3

Well I’m having problems with the javascript mask for the phone field, I’m developing mobile and I’m using the codes below.

input code:

<input type="text" name="telefone" id="telefone" maxlength="15"/>

javascript code

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

Console error:

Uncaught TypeError: undefined is not a function //Erro na linha do .maks(...)

What can it be ? And how to fix it, someone has some better idea ?

I need him to check if there are any lyrics and proper mask.

Thanks in advance.

  • You got the plugin charged ?

  • It seems to be running before the body completes the upload. So it does not find the object $('#telefone') and so there’s something like undefined.mask('(00) 0000-00009').. It is obvious that there will be no function/method for undefined.. But it is only a suspicion. It may be another cause.

  • Daniel, this could probably be it. I’ll check when I get home

2 answers

0


You must be forgetting to add the library call

<script type="text/javascript" src="js/jquery.mask.min.js"/></script>

Then just do something like this

<label for="txttelefone">Telefone</label>

<script type="text/javascript">$("#txttelefone").mask("(00) 0000-00009");</script>

0

Check if you have put the jQuery plugin and Masked Input in that order, then start the plugin inside:

(function($) {
    $(document).ready(function() {
        $('#telefone').mask('(00) 0000-00009'); 
    });
}).(jQuery);

Behold this page, example 4.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.