28
One of the often appearing tasks during making user interface for website is the control of the data, entered by users into the input fields. For example, phone number, SSN, date should has certain format.
Masked input plugin allow you easy control what user can enter in input field on client side. Of course, it should not replace serever-side checking, you have to use both methods together but this is other theme to talk about.
This plugin allow you to use alpha, numeric and alphanumeric characters, to change placeholders and to execute a function once the mask has been completed, if needed.
Detailed description of this plugin you can find on the it homepage, but I can show you just a small example:
Including jQuery library and plugin:
<script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="jquery.maskedinput.js"></script> |
Calling the mask function for those items you wish to have masked:
jQuery(function($){ $("#phone").mask("(999) 999-9999"); $("#date").mask("99/99/9999",{placeholder:" "}); $("#ssn").mask("999-99-9999",{completed:function(){ alert("You typed the following: "+this.val());}}); }) |
..and HTML code:
<form method="get" id="example" action="" /> <input type="text" id="phone" /><br /> <input type="text" id="date" /><br /> <input type="text" id="ssl" /><br /> </form> |
Open this example in separate window and view source code.
This plugin is good if you know how many characters exactly user shoud enter. But sometimes you need to allow or restrict user to enter variable quantity of certain characters. Another one great plugin, jQuery AlphaNumeric allow you to resolve this task. It allow you to use alpha, numeric or alphanumeric characters, some special characters, like dots, you can also allow or restrict using caps letters.
Another one example:
Including jQuery library and plugin:
<script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="jquery.alphanumeric.js"></script> |
Calling functions for items you want to control:
jQuery(function($){ $('#alphanum').alphanumeric(); //allow only alpha and numeric characters $('#alphanocaps').alpha({nocaps:true}); //allow only alpha lowercase characters $('#numwithdot').numeric({allow:"."}); //allow numeric characters and dots }) |
..and HTML code:
<form method="get" id="example" action="" /> <input type="text" id="alphanum" /><br /> <input type="text" id="alphanocaps" /><br /> <input type="text" id="numwithdot" /><br /> </form> |
Open this example in separate window and view source code.
I think that usage of Masked Input and jQuery AlphaNumeric plugins is completely enough for handling the task of controlling the characters that the user can input in form fields.
Alex
March 4th, 2009 at 7:04 pm
I have noticed that with this plugin, underscores are allowed and even when I add it to the ichar list that is supposed to be available for use to make custom exception lists it still does this… the examples on the demo page do the same, do you know any way around this?
April 7th, 2009 at 5:05 pm
Not very useful cause not only underscore “_” exception. What I need to allow only one dot in decimal number? Alphanumeric plugin allow me to enter something like this 0…….3 and there is no way to allow only one dot in variable decimal number lenght. So stupid.
June 15th, 2009 at 6:05 pm
alphanumeric sucks, is it that hard to allow only one comma? yes, it is.
However i found something that can do it
http://www.texotela.co.uk/code/jquery/numeric/
Add A Comment