23

When trying to use the jQuery mask() function, I get the following error in the console:

TypeError: $(...).mask is not a function

This is a sample of my code where this is happening:

<html>
<body>
<input type='text' id='phone' />
</body>

<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>

<script>
//alert($);
$(document).ready(function(){

        $("#phone").mask("(99) 9999-9999");
});
</script>

</html>

How do I fix this?

Robin James Kerrison
  • 1,727
  • 1
  • 15
  • 26
Jack
  • 16,276
  • 55
  • 159
  • 284

3 Answers3

34

Jquery mask is a plugin. You can directly add this line in your HTML if you want to use a CDN version:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.10/jquery.mask.js"></script>

Another way is to use a package manager like npm or bower. To accomplish that, follow the instructions in the README.md file of the project.

Gnucki
  • 5,043
  • 2
  • 29
  • 44
  • Thanks. I throught this was built-in in the jquery. – Jack Feb 14 '15 at 06:17
  • 2
    You should not use the link to a JavaScript in a gihub format. You should use its minimized version publicly available or store it in a local file. – Sigismundus Feb 14 '15 at 06:23
  • If I use mask "(99) 9999?9-9999" and type `123456789` I get the literally the `?` symbol: `(12) 3456?7-89`. How fix it? – Jack Feb 14 '15 at 06:23
  • @Sigismundus: It was for testing only. Since it worked, I download it to a local file. – Jack Feb 14 '15 at 06:24
  • OK - if you do not need ? character then you can remove it. – Sigismundus Feb 14 '15 at 06:25
  • Yes you have to do the minifying process on your own server here. I'm sorry but I never used this plugin so I just can give you the link to some documentation: http://igorescobar.github.io/jQuery-Mask-Plugin/ – Gnucki Feb 14 '15 at 06:26
  • @Sigismundus: I suposed it's for make the number optional. – Jack Feb 14 '15 at 06:27
  • It seems you must use `#` instead of `?`. – Gnucki Feb 14 '15 at 06:28
  • You could use [patterns](http://igorescobar.github.io/jQuery-Mask-Plugin/) i.e. for numbers {pattern: /[0-9*]/} – Sigismundus Feb 14 '15 at 06:29
21

Change this line from

$(document).ready(function(){

to

$(document).ready(function($){

RAHUL KRISHNA
  • 219
  • 2
  • 2
2

jQuery itself does not provide functionality for masking an input. You can use one of the plugins avaiable for it.

Sigismundus
  • 635
  • 4
  • 15