JQuery Masked Input Plugin doesn't accept paste

12,096

Solution 1

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(''); });

Solution 2

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.

Solution 3

Another solution: remove maxlength attribute from your input. plugin cuts extra symbols itself, but you will be able to paste from buffer correctly.

Solution 4

Had the same issue, the most painless solution I found, without removing anything (placeholders, etc) is:

$('input[placeholder]').on('paste', function(){
    $(this).val(' ');
});

Works as a charm :)

One more solution I found:

$('input[placeholder]').on('paste', function(e){
    e.preventDefault();
    var clipboardCurrentData = (e.originalEvent || e).clipboardData.getData('text/plain');
    window.document.execCommand('insertText', false, clipboardCurrentData);
});

Solution 5

Try Formatter.js instead. I switched for this exact reason. Another reason was bad UX surrounding incomplete fields. When I go from one window to another to get a phone number, for instance, I might add three numbers, change windows to get the rest of the number, then return, but the masked input plugin clears the field each time I change windows. Frustrating UX!

Share:
12,096
jguilhermemv
Author by

jguilhermemv

Solid academic formation and great knowledge/experience at Java Platform, specially using J2EE in big projects such as ERPs for sanitation and universities. In my early career , I had a great experience at IT Support, assembling and maintaining computers which was great to consolidate my theorical knowledge regarding to computer hardware. As a self-taught I learned other programming languages such as PHP, Delphi, C, Grails, among many others, which gives me flexibility to develop complex projects and integrate with legacy software. I love reading and studying about technology in general, from programming languages and hardware to interface design/usability. For me, reading is a pleasure and is also necessary to keep me up with the cutting edge technologies. That said I learned by myself how to operate graphics softwares like Photoshop, Illustrator, Adobe Premiere, Adobe After Effects, 3D Studio Max among others. Though in an amateur way, the experience I had using these softwares gave me a great versatility to develop whatever I want. If I don't know something, at least I know where to look for it. I'm a J2EE Specialist with experience using frameworks like EJB2/3, Hibernate, JSF, Struts and Spring, as using application and web servers like Apache Tomcat, JBoss, Weblogic and Apache Web Server. Also with knowledge in deploying applications with Ant/Maven, configuration management/version control using SVN/CVS/Git, bugtracking with Bugzilla and Mantis and last but not least continuous integration with Hudson. I'm very experienced at configuring, managing and deploying applications to Amazon EC2 instances and S3 Storages. In the design area I have knowledge of webdesign using HTML with CSS and Javascript/JQuery/AJAX. Nowadays, I'm working at a government company as Java EE Architect with big challenges like building a Hadoop as a service platform for Big Data, Business Intelligence and Data Mining projects.

Updated on June 24, 2022

Comments

  • jguilhermemv
    jguilhermemv about 2 years

    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.