iMask

by Fabio Zendhi Nagao (http://zend.lojcomm.com.br)

iMask is an open source (free) javascript tool for creating input and textarea masking.

index:

  1. Index
  2. Abstract
  3. Examples
  4. Browser compatibility
  5. Features
  6. Usage
  7. License
  8. Download
  9. Version history
  10. Comments

abstract:

Who haven't ever wanted to apply an input mask to an HTML form field? This very common feature in traditional GUI applications is not natively supported by web applications. iMask goal is to implement an easy way for developers to add mask into their form fields, increasing the database and software consistency with standard compliant XHTML and unobtrusive JavaScript.

examples:

browser compatibility:

iMask is compatible and tested in Internet Explorer, Firefox (and its mozilla friends), Opera and Safari (thanks macspyre for testing it). But it should work in other browsers too... If you successfully find another compatible browser, please let me know ;)

features:

1. keyboard control

Special keys functions

2. dynamic fixed mask definition

Create your own masks for type "fixed" using [9, a, x] notation:

9 := numerical symbols.
Default set is: "123456789"

A := alphabetical symbols.
Default set is: "abcdefghijklmnopqrstuvwxyz"

X := alphanumerical symbols.
Default set is both alphabetical and numerical symbols.

3. event handling

Integrate with other components and create rich interactive interfaces. iMask class can be initialized with the following customizable methods:

  • onFocus - fires when field get focus
  • onBlur - fires when field lost focus
  • onValid - fires when user press a valid char
  • onInvalid - fires when user press a invalid char
  • onKeyDown - fires at every key press

4. dynamically charset definition

Want to extend the default charset for a card? [9, a, x]

Initialize iMask with your own set of validNumbers, validAlphas and validAlphaNums!

how to use:

First of all, iMask is built over MooTools v1.1, so both libraries are required.
Get MooTools at http://mootools.net and iMask here.
With scripts in hands, include them between your "head" definition:

Initialize the iMask class:

That's it, at this rate you should be able to use iMask. To enable a mask for a field, just add the properties class="iMask" and alt="<options>".

<options> is a string representing an object in JavaScript Object Notation (aka. JSON). Here is the list of valid properties:

  • type : (string) fixed || number.
  • mask : (string) your mask using [ 9, a, x ] notation.
  • stripMask : (boolean) true || false.

Nothing better than an example to explain a tool usage, so here is the code behind this page examples:

license:

This piece of code is is released under the Open Source MIT license, which permits you to use it and modify it in every circumstance. For more details read it below:

The MIT License

Copyright (c) 2007 Fabio Zendhi Nagao - http://zend.lojcomm.com.br

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.

download:

iMask is available in the formats below:

version history:

05/22/2007 : v1.0 - first public release

03/16/2007 : v0.002

03/11/2007 : v0.001

comments:

User ratingFirst release3/10/2007 2:06:20 AM
Author:Fabio Zendhi Nagao(http://zend.lojcomm.com.br/)
After a lot of research and documentation (the worst part oO;)
I'm proud to make public this first version of iMask. I hope it turns our developers life easier.
User ratingNice indeed3/21/2007 5:29:13 PM
Author:bjorn
Hi! really nice stuff, i will defenetley use it in a future site of mine... Got a nother question to ya. You smoothscroll. Is there by any chance a possebility for you to show me or explain how you made that? From what i know you might have gone from mootools? like that bumpy effect on the end =) Thx for sharing!
User ratingNot working ...3/23/2007 7:10:41 AM
Author:Peter PRESKA
Hi Fabio,
I am trying to use your script in my form fields but getting this error
from Javascript console:

this.setOptions is not a function
initialize(Object)imask.js (line 43)
e()mootools.js (line 2)
[Break on this error] this.setOptions(this.defaultOptions(), options);
User ratingGreat lib3/26/2007 2:45:26 PM
Author:Ronie Uliana
I have found no one better than this!

Awesome! Keep the good work! :)
User ratingGreat!4/16/2007 10:33:34 AM
Author:Leandro Almeida
It seems a great script. But i dont like one thing: the backspace should behave as del key, removing the characters...

Is there a way to alter it?

Valeu cara, bom trabalho!
User ratingGreat work.. but...4/21/2007 10:28:17 PM
Author:Adam
Hi there, Great stuff.... however I have a problem when I set stripMask: false - I can no longer enter any data.. this is being tested on mac FF2.
User ratingTwo lil' things..5/6/2007 2:07:13 PM
Author:Steve(http://inscrutable-exhortations.com)
Any way you can add in a handler for shift+tab to move backwards between fields? It works with normal forms, and is nice when you want to go back.. More useful because of the second suggestion:

Auto-tab. Would be a great addition.

Aside from those, and even without them, this is gold man, pure gold.
Hi Steve,

As you can see in the version history, your suggestions are already implemented:
@v1.001 [added] enabled tabbing between fields @tab
@v1.002 [added] enabled tabbing to previous field @shift+tab

Please, if they are not working for you, let me know your config.

Best Regards, Fabio Zendhi Nagao @ 05/07/2007 0h18
User ratingNice but,5/23/2007 6:49:20 AM
Author:Patrick(nositeyet)
Nice but what i seem to be missing is a max-length on the money field, it totally ignores the input maxlength.
User ratingExactly what I am looking for5/26/2007 1:14:51 AM
Author:Siva
Excellent Job.

This is exactly what I am looking for. Linking this fValidator makes the web-development easy and fun.

Appreciate your efforts and time.

Simple thank you is not a measure for your efforts.

Cheers
Siva
User rating2 things missing5/31/2007 9:58:09 AM
Author:nehnard
This great script has two missing features.
1. negative numbers
- sign is not supported for type:'number'
2. copy paste to/from clipboard
Yes, it's true... i've not solved the clipboard problem yet, I saw some approachs from the other mootools developers though... i'll be updating as soon i can.
User ratingGreat start6/4/2007 6:56:04 AM
Author:ishtov(http://www.theKehila.com)
Looks like a great script.
Until now have been using Adobe Spry together with Mootools, which just just hurts so much. While A. Newton does has a form validator, this is what we need!
I really admire you guys that write these stuff - I have enough troubles just getting it to work. :)
Looking forward to updates (perhaps you can rip the ideas - but not the verbose code - off of spry?):
A way to make the backspace key erase - curent method is confusing.
The ability to highlight the whole string and copy it.
The ability to select any character by cliking it.
A blinking cursor [even a fake one by using the pipe with .periodical()]
Why is it showing me the brackets in the first example (I do not understand, must be feature).
Gee whiz, I ought to play with this a bit more before posting. Am using FF1.5 on XP.
Thanks though.

Thanks for your suggestions! Wait for the next release xD
User ratingOpera6/7/2007 6:48:47 AM
Author:Peter(kikoz.ru)
In Opera I can delete characters and mask goes wrong - these characters places gets removed and never come back.Hi, it's true. Somehow Opera delete event behaves in a way that I'm not able to stop it's event from propagating. If you have an idea on how to handle it, please let me know.
User ratingTabbing bug6/11/2007 2:40:04 PM
Author:Bruno Daniel(www.vault7.com)
I found that when you hold tab to quickly navigate through all the fields in a form, and two or more iMask-enabled fields are in the way, they'll swap focus in a infinite loop...

Anyway, thanks for this great tool, I'm using it in in some of my projects :D
User ratingCan't enter data if stripMask=false and no initial value6/27/2007 5:44:54 PM
Author:Leandro(scrum.com.br)
Hi Fábio,

Congrats, this is great work!

I'm testing with dates and I can't enter any data when stripMask='false' and the field has no initial value. Is this a bug/feature?

If you change stripMask to true, then it works, but it has an awkward effect on dates when the field looses focus (from: 01/01/2001 to: 01012001).

Cheers.
User ratingusing iMask with Ajax6/27/2007 6:30:17 PM
Author:Leandro(scrum.com.br)
I'm trying to use iMask with fields loaded dynamically using ajax.
The problem, which I guess is expected, is that the fields are not "parsed" or "initialized" by iMask, because they don't event exist when iMask was created.

Is there any way I can "add" fields to iMask after it's creation?

Cheers!
User ratingStripmask7/18/2007 10:21:18 AM
Author:Alexander
At first I thought this iMask thing is great, but I run into a nasty bug. When I set stripMask to false, the entire thing doesn't work anymore. I tested this on IE6.
User ratingstripMask7/27/2007 2:30:40 PM
Author:Johnny
Hey man, vou escrever em pt mesmo hein...

Obrigado por suas contribuições, bom gostaria de sugerir algo, penso que poderia ter mais um parâmetro para deixar o stripMask:true e no onBlur o campo permaner formatado com a máscara.

E estou com outro problema que me parece um bug, qdo utilizo {type:'fixed',mask:'999.999.999-99',stripMask: false} é obrigatório o campo ter um value inicial?

Abraço.
User ratingA great job7/30/2007 3:21:30 PM
Author:Juan Talavera(www.netvision.com.py)
finally a serious script for this stuff. thx.
User ratingTabbing Bug8/2/2007 2:50:35 PM
Author:Skidank
I can also confirm there appears to be some sort of tabbing bug in both IE7 and Firefox 2. I think the bug happens when tabbing between masked textfields and mouse clicking to another masked textfield at the same time in an out of tab order sequence. The javascript keeps switching focus between textfields indefinitely and doesn't allow the user to gain back control until the user reloads the page or switches to a different tabbed webpage.

Besides that one bug, the masked library works great.
User ratingCopy & Paste8/4/2007 9:12:53 PM
Author:André Fiedler(www.visualdrugs.net)
is it possible to implement past? I mean, to fetch the pastet string and try to input it.. char by char? Copy would also be nice, with added sepparators (if used). ;o)

Did you like it? Disliked it? Express your feelings, leave-me a message. (Yellow fields are required)

© 2007-2009 Fabio Zendhi Nagao (nagaozen) - http://zend.lojcomm.com.br/