Ray Cheng
Ray Cheng

Reputation: 12576

MaskedInput with two optional sections not working

I'm using MaskedInput plugin for jQuery to create an input mask that has two optional sections.

a?a-a?a where the second character is optional.

But MaskedInput always sees it as invalid input if I enter a-a even the second character is it's marked as optional.

fiddle

Upvotes: 0

Views: 946

Answers (1)

Avner Solomon
Avner Solomon

Reputation: 1506

If you read the documentation carefully … which I just did since I never used this plugin, it clearly says that http://digitalbush.com/projects/masked-input-plugin/ :

You can have part of your mask be optional. Anything listed after '?' within the mask is considered optional user input. The common example for this is phone number + optional extension.

So you can't do what you want with ? but you can use this:

If your requirements aren't met by the predefined placeholders, you can always add your own. For example, maybe you need a mask to only allow hexadecimal characters. You can add your own definition for a placeholder, say 'h', like so: $.mask.definitions['h'] = "[A-Fa-f0-9]"; Then you can use that to mask for something like css colors.

So we can define a mask for alpha + space

$.mask.definitions['h'] = "[A-Za-z ]"
$('#blah').mask('ah-ah');

http://jsfiddle.net/ERfz4/

the problem with this code is that it's not quite what you want. It will accept "aa-aa" and "a -a " but not "a-a" or "a -a"

I think that for what you need you might want to try to implement it yourself and not use this plugin.

Upvotes: 1

Related Questions