meioMask – a jQuery mask plugin
Notice
This project wont be continued. Instead theres a Mootools version that is way more stable and complete.
I’ve done this because i realized that with Mootools i can organize my code better. Take a look at the code of both the jQuery and Mootools versions so you can understand what i mean.
How to use
Menu
- Download it!
- Features
- Compatibility
- Usage
- Options
- FixedChars
- Rules
- Masks
- Demos
- Changelog
- License
- Post Comment
meioMask download
Encoding is important if you are going to use the mask with special characters like áéã (rule ‘@’, see below for more). If you are going to allow these characters at your masked inputs and your page enconding is not UTF8, then you should add the charset attribute with value UTF8 to your script tag, like so:
<script type="text/javascript" src="jquery.meiomask.js" charset="utf-8"></script>YUI Compressor
meioMask features
- Accepts paste event;
- Has fixed and reverse mask types ( allow number mask );
- You can still use your hotkeys and others (ex: ctrl+t, ctrl+f5, TAB …);
- Supports metadata plugin;
- Works with iPhone;
- Allow default values;
- Has callbacks for invalid inputs, valid and overflow;
- Has function to mask strings;
- And much more!
meioMask compatibility
meioMask has been tested with jQuery 1.2.6 and 1.3.x on all major browsers:
- Firefox2, Firefox3, Firefox3.5 (Win, Mac, Linux);
- IE6, IE7, IE8 (Win);
- Chrome (Win, Mac, Linux);
- Safari3.2, Safari4 (Win, Mac, iPhone, yes, it supports iPhone!);
- Opera (Win, Mac, Linux).
meioMask usage
It’s a snap to use:
<!--include jquery library and meioMask plugin -->
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.meiomask.js" charset="utf-8" ></script></p>
<script type="text/javascript" >
(function($){
// call setMask function on the document.ready event
$(function(){
$('input:text').setMask();
}
);
})(jQuery);
</script>
<p>
meioMask options
It just has the options bellow that will be applyed by default if you don’t specify any.
$.mask.options = options : {
attr: 'alt', // an attr to look for the mask name or the mask itself
mask: null, // the mask to be used on the input
type: 'fixed', // the mask of this mask
maxLength: -1, // the maxLength of the mask
defaultValue: '', // the default value for this input
textAlign: true, // to use or not to use textAlign on the input
selectCharsOnFocus: true, //selects characters on focus of the input
setSize: false, // sets the input size based on the length of the mask (work with fixed and reverse masks only)
autoTab: true, // auto focus the next form element
fixedChars : '[(),.:/ -]', // fixed chars to be used on the masks.
onInvalid : function(){},
onValid : function(){},
onOverflow : function(){}
};
meioMask fixed characters
FixedChars, as the name says, are the fixed characters that you will have on your mask. For example if you mask is ‘99/99/99′ you will probably want to have the ‘/’ to be a fixedChar. So the fixedChars value can be ‘[/]‘.
By default meiomask has some fixed characters that can be used to make the masks. You can change the regular expression that matches the fixed chars by changing the ‘fixedChars’ string. The default one is ‘[(),.:/ -]‘.
meioMask rules
Rules are symbolic characters that match a certain regular expression. meioMask has some defined rules but you may add yours to the rules object.
Making masks will be explained further, and you’ll understand how to glue all this stuff
.
meioMask defined rules:
$.mask.rules = {
'z': /[a-z]/,
'Z': /[A-Z]/,
'a': /[a-zA-Z]/,
'*': /[0-9a-zA-Z]/,
'@': /[0-9a-zA-ZçÇáàãéèíìóòõúùü]/
};
!! There are still the number rules. They will be created ‘on-the-fly’ so if you create a rule that is a number it will be overwritten!
Number rules are:
$.mask.rules = {
'0': /[0]/,
'1': /[0-1]/,
'2': /[0-2]/,
'3': /[0-3]/,
'4': /[0-4]/,
'5': /[0-5]/,
'6': /[0-6]/,
'7': /[0-7]/,
'8': /[0-8]/,
'9': /[0-9]/
};
meioMask masks
Defined masks:
$.mask.masks : {
'phone' : { mask : '(99) 9999-9999' },
'phone-us' : { mask : '(999) 9999-9999' },
'cpf' : { mask : '999.999.999-99' },
'cnpj' : { mask : '99.999.999/9999-99' },
'date' : { mask : '39/19/9999' }, //uk date
'date-us' : { mask : '19/39/9999' },
'cep' : { mask : '99999-999' },
'time' : { mask : '29:69' },
'cc' : { mask : '9999 9999 9999 9999' }, //credit card mask
'integer' : { mask : '999.999.999.999', type : 'reverse' },
'decimal' : { mask : '99,999.999.999.999', type : 'reverse', defaultValue: '000' },
'decimal-us' : { mask : '99.999,999,999,999', type : 'reverse', defaultValue: '000' },
'signed-decimal' : { mask : '99,999.999.999.999', type : 'reverse', defaultValue : '+000' },
'signed-decimal-us' : { mask : '99,999.999.999.999', type : 'reverse', defaultValue : '+000' }
}
You may add yours to the $.mask.masks Object. Like this:
$.mask.masks.msk = {mask: '999'}
This code will add a mask called ‘msk’ to the masks Object. Now you can use an input with the ‘attr’ atribute set to this new mask. Like this:
All the options from the plugin are accepted to be used at a mask you define.
If you want to add more than one mask it will be better to do this:
$.mask.masks = $.extend($.mask.masks,{
msk:{ mask: '999' },
other_msk:{ mask: '6666' },
another_one:{ mask: '7777' , type:'reverse' }
});
You can put any other options on a new mask that you create (fixedChars for example.)
meioMask demos
standalone
meioMask paste feature demo
We now will see the paste feature of this plugin, and how nice it is. I want you to copy some texts and paste them on the input. You’ll see that it just pastes what is corret for the field.
They will all give the same result! Imagine your visitor using it! It’s the way he want’s it to be!
with metadata plugin
You can always change metadata plugin settings. See they’re page for more information.
Metadata will always overwrite the ‘attr’ value. Look these examples to make it more clear.
change mask on-the-fly
mask string example
read-only inputs
callbacks example
signed masks example
‘repeat’ mask type example
This mask makes it possible to have values with infinite or defined length in a input. It doesn’t support fixedChars.
Note: both the attribute maxLength and the option maxLength will work for setting the max length for the repeat mask.
$().unmaskedVal example (deprecated)
!!! This function has been deprecated, because it does not work correctly for all the masks but it should be easy for you to implement yours. !!! This function removes all the FixedChars from the value of the input and returns it.
Changelog
v1.1.2
- Set defaultValue property of the input. This fixes the behavior of the reset button on forms. Now the value will be reseted to the masked value;
- âêô were added to the ‘@’ rule;
- Fix for the auto-tab feature, it now focus just on visible form elements;
- Added setSize option. It sets the input size based on the length of the mask (work with fixed and reverse masks only).
v1.1.1
- Fixed caret bug on ‘repeat’ masks;
- Fixed keyup event on fixed masks, it is now firing propertly;
- Added selectCharsOnFocus option;
- Added textAlign option.
v1.1
- Mask type ‘infinite’ is now called ‘repeat’ (using ‘infinite’ still works but it is deprecated). It now allows a maxLenght value to be set. MaxLength can be set by the maxLength attribute of the element or the maxLength option;
- You can easily set an autoTab option that will focus the next form element when the masked input is totally filled. It is true by default but you can put a jQuery selector string to match the next element you want to be focused.
- Deprecated ‘unmaskVal’ function. This function is too buggy… works for most cases but not all. The best way to unmask a value is by doing it yourself;
- The fixedChars option is not global anymore, giving more flexibility for the masks;
- ‘phone-us’ mask is now ‘(999) 999-9999′;
- Correctly fires the onChange event on reverse masked inputs.
v1.0.4
- New mask type ‘infinite’, it allows infinite values at masks. See demos for a better understanding;
- Added new function ‘unmaskVal’ that returns the input value without the mask. See demos for a better understanding;
- Removed a serious bug at IE that was fixing the caret at the end of the input;
v1.0.3
- Callback functions now receive the char number as it second parameter, so it is possible to detect exactly which key has been pressed;
- Added the signed masks. It only works with the reverse mask. See demos for more details;
- iPhone support improved. iPhone now works 100% better than the 1.0.2 version. I’ts logic have been changed, and now it’s working with full features.
v1.0.2
- Added input callbacks: onInvalid, onValid and onOverflow;
- Added support for default values;
- Can now be used like $().setMask({});
- Added the function $.mask.string(string,mask) that will mask a string (see demos);
- Now the value of the input is masked at the time the mask is applyed to it.
v1.0.1
- Added support for iphone;
- Removed a bug where a fixed char would be replaced by an inputed one.
v1.0
- Initial release.
License
All this documentations and examples are under the Open Source MIT License.
The MIT License
Copyright (c) 2008 Fábio Miranda Costa
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


September 24th, 2008 at 15:39
Gostei desse plugin, soh uma duvida quanto às regras, onde estou tendo dificuldades para criar mascaras com letras, por exemplo uma placa de carro “ABC-0000″
September 24th, 2008 at 16:16
ok amigo, obrigado.
uma mascara desse tipo ficaria:
$.mask.masks.placa = {mask: ‘ZZZ-9999′} ou $.mask.masks.placa = {mask: ‘aaa-9999′}
O primeiro caso é um pouco ruim porque vai forçar o usuário a digitar as 3 letras iniciais em maiúsculo, o que pode causar confusão. nesse segundo caso você teria que colocar style=”text-transform:uppercase” no seu input de texto, mas aceita tanto minúsculo quanto maiúsculo, e o seu input estará sempre com letras maiúsculas, por causa do css que foi aplicado.
Abraço.
September 24th, 2008 at 17:43
Segui tua dica e deu certo oq eu queria, obrigado.
October 3rd, 2008 at 11:49
I was wondering if you could add (or give me some idea how to add) character styling if a person tried to type a “masked” character. For example: to make the field’s input or label or whatnot flash red to indicate an invalid character was being pressed.
October 3rd, 2008 at 11:51
Oh and I forgot to mention how awesome this plugin is. Beats the heck out of anything else jquery has for masking inputs. Many many thanks.
October 4th, 2008 at 00:29
thanks seth! i’m still working on some other feaures i want to put on the plugin but i’ll look at this nice feature you are sharing and tell you if it works ok. Have a nice day.
October 7th, 2008 at 16:18
I had made a call to $.mask.fixedChars=’[(),.:/ -x]‘; Which broke the application (timeout errors). Apparently placing the ‘x’ next to the ‘-’ specified a range. instead invoking: $.mask.fixedChars=’[(),.x:/ -]‘; resolved the problem as allowed the ‘x’ to be a fixed character in a mask.
October 11th, 2008 at 21:54
Yes, just a regular expression error.
October 13th, 2008 at 14:12
This is by far the best input masking plugin I’ve come across. There is just one feature I would really like to see added.
I needed a mask for currency and wanted the output to look like $9,999.99. It is just like your decimal number example except it has a ‘$’ in front. I couldn’t figure out how to do this with your plug in. Here is the mask I tried:
{ mask : ‘99.999,999,999,999$’, type : ‘reverse’ }
This only added a “$” when all 14 digits were entered. I ended up altering some functions and adding a new option that allowed this. My new mask looks like:
{ mask : ‘99.999,999,999,999′, type : ‘reverse’, suffix : ‘$’ } I’d be happy to help implement a more robust version of this feature if you want. Thanks again for your great plugin.
October 13th, 2008 at 14:45
Hm, i haven’t thought about that, i would like to see it in action, can you show me the site using the modification? If you could send the code by mail i would appreciate too! I’ll release the 1.1 version soon, i’m just doing some improvements on the code and changing the actual docs page to the projects page from the blog. Maybe i can add this feature of yours if it’s possible. Thanks again!