开发者

Allowing Dash with jQuery Masked Input Plugin

I was trying to mask a tex开发者_Python百科t field and took a look at http://digitalbush.com/projects/masked-input-plugin

I created a custom mask definition to allow for Lastname to have A-Za-Z and space.

How can I add a dash to the mask? I've tried escaping it like the following example:

$.mask.definitions['~'] = '[A-Z,a-z,\-, ]';

but it doesn't seem to work.


The definitions object is in essence a set of keyed regex character classes. The keys are the special mask edit characters. By default, if you look at the source code for the masked edit plugin, you'll see that it defines three (for '9', 'a', and '*'):

definitions: {
  '9': "[0-9]",
  'a': "[A-Za-z]",
  '*': "[A-Za-z0-9]"
}

So all you need to do is to declare a correct regex character class. Since '-' is a metacharacter for character classes (it defines a range within the class; like a-z) you have to place it first. So the answer would be

$.mask.definitions['~'] = '[-A-Za-z ]';

This would allow the '~' character within a mask to be replaced with an alpha character, a space, or a hyphen.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜