Search code examples
jquerymasking

JQuery mask not working properly


I have a problem with mask in jquery, I want to make a mask that will allows me to input any letter 0-n times. I have something like this:

$.mask.definitions['@']='[A-Za-z]';
$('#textMask').mask("@@@@@@@@@@");

But with my code I could insert only to ten characters. anyone have an idea If it is of course possible?

I think about something like this:

$('#textMask').mask("@*");

but this allows me to insert one letter and one no matter what character. e.g d5 etc. In brief I want to allow user to type a text containing only letters. e.g.

abcdEEEE
abRFdas
a
 //empty field
bfdjksgbjfdsgbjklfbjklfgnfljkgbnhlfsdjgnfdlskgnfdsgfhsdlgdf
...

etc.

I used JQuery 1.10.3 and plugin version 1.3.1 (http://digitalbush.com/projects/masked-input-plugin/)


Solution

  • With JQuery Mask plugin if you apply mask method on <input> field, automatically maxlength attribute is assigned to it. So when you do $('#textMask').mask("@*"); then you get something like <input id='textMask' maxlength='2'/>. To avoid this you can set maxlength attribute on mask. In the other hand to achieve the 0-n times you can use recursive attribute. So finally to accept letter 0-n times:

    $('#textMask').mask('@', { 'translation' : { '@' : { pattern : /[A-Za-z]/, recursive: true, }}, maxlength : false });
    

    Take in account that if you previously make a mask on your input field probably maxlength attribute is set and specifying maxlength : false not unset this attribute (I think this is like a bug), so previously apply unmask() on your element.

    EDIT BASED ON COMMENT:

    What version of JQuery and plugin mask are you using? I use 1.11.0 of JQuery and 1.7.4 of plugin mask and works correctly check this jfiddle

    Hope this helps,