0

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/)

LEEL
  • 25
  • 1
  • 8

1 Answers1

1

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,

albciff
  • 18,112
  • 4
  • 64
  • 89
  • #albciff no I didn't previously mask it. I paste the code which You post but it didn't work properly, it still works like I have $('#textMask').mask('@'); and I'm able only to type one letter. – LEEL Aug 21 '14 at 08:58
  • @LEEL this works for me, check your jquery and mask plugin version. If you want check it out jfiddle sample that I put in the answer. – albciff Aug 21 '14 at 09:10
  • maybe it's because I don't use html input but grails textfield. ? #edit If I changed to input nothing happened :c – LEEL Aug 21 '14 at 09:16
  • Okey I have no more patience for this and changed my prev mask plugin to this one what You post @albciff. Thanks :)! – LEEL Aug 21 '14 at 09:38
  • I see you last answer edit, you are using a different mask plugin (https://raw.githubusercontent.com/digitalBush/jquery.maskedinput/1.3.1/dist/jquery.maskedinput.js), If it's possible try with http://igorescobar.github.io/jQuery-Mask-Plugin/ – albciff Aug 21 '14 at 09:38