Autotab is a jQuery plugin that provides auto-tabbing and filtering on text fields in a form. Once the maximum number of characters has been reached within a defined text fields, the focus is automatically set to the defined target of the element. Likewise, clearing out the text field's content by pressing backspace eventually places the focus on the elements previous target. The purpose of this script is to easily establish auto-tabbing between multiple text fields. It is not intended to support formatting a single text field to a specific need; an example of this is formatting a 10 digit US phone number as (800)555-1234 or a Social Security Number as (123-45-6789).
Download Autotab 1.0 | May 22, 2008 | Dual License: MIT and GPL
Test out the various settings of Autotab below, each noted with their specifications.
<script type="text/javascript" src="jquery.autotab.js"></script>
<form>
<div><strong>Phone Number:</strong></div>
<input type="text" name="area_code" id="area_code" maxlength="3" size="3" /> -
<input type="text" name="number1" id="number1" maxlength="3" size="3" /> -
<input type="text" name="number2" id="number2" maxlength="4" size="5" />
</form>
<script type="text/javascript">
$(document).ready(function() {
$('#area_code').autotab({ target: 'number1', format: 'numeric' });
$('#number1').autotab({ target: 'number2', format: 'numeric', previous: 'area_code' });
$('#number2').autotab({ previous: 'number1', format: 'numeric' });
});
</script>
Autotab allows a certain level of flexibility in how you initialize the auto-tabbing mechanism. Here is a modified version of the above example that will yield the same results:
<form>
<div><strong>Phone Number:</strong></div>
<input type="text" name="area_code" size="3" /> -
<input type="text" name="number1" size="3" /> -
<input type="text" name="number2" size="5" />
</form>
<script type="text/javascript">
$(document).ready(function() {
$('input[name=area_code]').autotab({ target: 'number1', maxlength: 3, format: 'numeric' });
$('input[name=number1]').autotab({ target: 'number2', maxlength 3, format: 'numeric', previous: 'area_code' });
$('input[name=number2]').autotab({ previous: 'number1', maxlength 4, format: 'numeric' });
});
</script>
This example provides the benefit that you have less HTML, but the drawback is that you have to modify your selector and specify the maxlength of the field. Six here, half a dozen there; it comes down to preference.
Comments
Format callbacks
Thanks for this plugin!
Some suggestions:
I'd like to have a filter for (possibly localized) decimal numbers.
How about an option to give a custom formatter callback?
E.g. in your case statement something like this:
...
default:
if (typeof(defaults.format) == 'function')
var val = defaults.format.apply(this, [val]);
break;
...
Also, It'd be nice if the plugin could find a default target using the default tab order on its own, sort of like the fluidTyping plugin.
Interesting feature
I never heard of the fluidTyping plugin, so I decided to check it out and noticed something: when you press backspace in an empty text field, the last character in the previous text field would be removed. I wondered if this was proper behavior or not so I asked two people, who agreed that it seemed proper. I then loaded up an XP install on Virtual PC to see how the product key entry text fields behaved when doing the same actions. My suspicions were correct in that the last character was removed, so I have modified the code to behave this way, which means an Autotab update is coming, but not until I address your other suggestions.
A while back I created two versions of this plugin. The second version works identically to the release, but applying filters was in mass and tabbing was handled automatically. Using the examples listed above:
<script type="text/javascript">
$(document).ready(function() {
$('#area_code,#number1,#number2,#ssn1,#ssn2,#ssn3').autotab_filter('numeric');
$('#alphabet1,#alphabet2,#alphabet3').autotab_filter('alphabet');
$('#alpha1,#alpha2,#alpha3,#alpha4,#alpha5').autotab_filter({ format: 'alpha', no_space: true });
$('#alphanumeric1,#alphanumeric2,#alphanumeric3,#alphanumeric4,#alphanumeric5').autotab_filter({ format: 'alphanumeric', uppercase: true, no_space: true });
$(':input').autotab_magic();
});
</script>
The second version also included various other filtering options, such as symbol/special (!@#$%^&* and so on), money/currency and a different filtering method for alpha. The idea of passing a function is good, but I'll take it a step further so that you could specify a pattern of your own (default.format = 'custom', default.pattern = '[^0-9A-Z]') or pass a function; this should offer a new level of flexibility to the developer, which is always a good thing.
The current state of Autotab makes it difficult to apply previous and next targets to a text field, but if I create functions out of the moving action, or somehow make it possible to alter the targets after the autotab settings have been applied, then this should be doable. Notice in the code above I have a function called autotab_magic(), which does exactly what you're suggesting. I'll get this incorporated somehow in the next release, which will probably be a few weeks off since I'll have a newborn child to tend to very soon.
Thanks so much for the input, I greatly appreciate it!
nice one
This exactly what i was looking for. Thanks for saving my time...
Great Share
Me too, saved me a bit of work today!
Nice blog too, you've got some other very helpful stuff here.
I appreciate it
I appreciate your kind words. I'm hoping to be able to provide useful information in the near future with tutorials or quick how to's. There are already a ton of these for various tasks on plenty of other sites, but I want to post those unusual, yet useful, things. I have something coming up soon about CakePHP and setting up multiple sites, but with a twist. More on that when I get around to fleshing out the full article.
Very userfull post. Nice
Very userfull post. Nice hack
Very nice script
I was looking for something like this, I was not even sure this was possible to do. I love that the Backspace button works :)
Suggestions for next version if you want to update would be to add support for pasting, as of now if I paste in my phone number it only puts in the first part and it does not jump to the next fields automatically.
Thanks a lot.
Guy
An update is coming... eventually
An update is coming, though I have had a number of new projects come up so this has been put on the back burner. Paste support will be added, be sure of that. I want to fine tune it so that it makes sense when people use it, and to give developers the best level of flexibility and customization possible. Sometimes I look at other people's scripts, which are half the size of mine, and wonder what in the world I was thinking! Thanks for your feedback, I really appreciate it!
Numeric with decimal
I was trying numeric which doesnt allow decimal. So i just add
case 'decimalNumber':
var pattern = new RegExp('[^0-9\.]+', 'g');
var val = val.replace(pattern, '');
break;
Can someone give me a regualer expression which just allow a number followed by dot and than a number (22.4 / 22.34)
it should not allow (23. / 23.43.24 etc)
This one's tricky
I'm not an expert by any stretch of the imagination with regular expressions, but I always like to jump at the opportunity to expand my knowledge of them. I like to use The Regex Coach to test out any expressions that I may need to create, and I was able to create something that would work in a language other than JavaScript. What I ended up with is:
[0-9,]+[.]+[0-9]{2}If the string in question is 23,192.168.531.110, the above expression would return 23,192.16, but like I noted, this won't work in JS. I did some searching for floating number regular expressions, but none of the many I tried were able to work properly; still, this shouldn't discourage you from searching and tinkering yourself. A second way to do it is to split the string up by the decimal and then join the first and second items together after applying, but this isn't ideal; another way would be to parseFloat() on the string, but again, this is not ideal. I'll see if there's a way that I can have this work properly, though the point of this script is to split fields up by characters such as a decimal point, so you'd end up with something like:
.
Very Nice Job
Good Luck!!!
Post new comment