Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Masked Input Plugin doesn't accept paste

First of all, I'm using:

  • Jquery 1.8.3
  • Masked Input Plugin 1.3.1 (The plugin can be found here)

My goal:

  • Mask an input field to accept only 11 numeric digits. However I want it to accept copy and paste in a way that if I copy a text containing only 11 numbers from an external source (e.g text file), the plugin let me paste in the masked input field.

The problem:

  • When I try to paste for example: 03073870970 (11 numbers), the mask only accepts 030738709 (9 numbers).

What should I do to solve this? Any tips are welcome.

like image 462
jguilhermemv Avatar asked Nov 27 '22 14:11

jguilhermemv


2 Answers

You dont need to change your plugin. You could bind paste event and clear the content just before the paste. So the mask wont be keeping any spaces to prevent you from making your paste.

$('input.class').bind('paste', function () { $(this).val(''); });
like image 62
Demeter Dimitri Avatar answered Dec 09 '22 21:12

Demeter Dimitri


Solution:

Change the placeholder from "_" or " " or any other placeholder to "" (empty string), as below:

$(".cpfInput").mask("99999999999",{placeholder:""});

The trick is that if you put any placeholder different of an empty string, the plugin will fill the input field with the placeholder and when you paste something it doesn't clean it before pasting whatever you're trying to paste.

like image 43
jguilhermemv Avatar answered Dec 09 '22 22:12

jguilhermemv