views:

44

answers:

3

I have a website that prompt the users to enter serial number for a product.

I have one text box and the user needs to enter the serial in this format:

xx:xx:xx:xx:xx:xx

Is there any component that will enter a : after every two characters?

Or maybe I should split the text box to 6 text boxes?

Alternatively are there any other techniques that you can suggest?

+3  A: 

You could try this jquery plugin, it's effectively a masked edit box, you should be able to set your pattern and have the input conform to it, I strongly suggest you check out the demo tab.

The code seems very simple to implement, yours will be:

jQuery(function($){
    $("#serial").mask("99:99:99:99:99:99",{placeholder:"_"});
});

Of course if your serial requires alphanumeric input, use * instead of 9.

http://digitalbush.com/projects/masked-input-plugin/#demo

Alternatively you could use 6 input boxes, I've seen this done before and it can look pretty good, I guess that is down to personal preference.

ILMV
A: 

You can use any mask-plugin for jQuery. Maybe this?

Mickel
+2  A: 

If you find that a plugin would be overhead, something like this would be very simple to create in jQuery.

I mocked up an implementation that checks if the value (stripping away the colons) is divisible by 2, and in case it is, it appends a colon to the value:

$(document).ready(function() {

  $('#serial').keyup(function() {
    var $this = $(this);
    if($this.val().replace(/:/g, '').length % 2 == 0) {
      if($this.val().length >= 17) return;
      $this.val($this.val() + ':');
    } 
  });

});​

Working example

David Hedlund
+1, I've always found the masked edit box plugins leave a lot to be desired - it's nice to see a simple alternative like this.
Andy E
Fair enough masked edit boxes aren't always what they're cracked up to be, but your working example doesn't work. I'm using Chrome and when I type in 3 characters then try and backspace it deletes the `:` and puts it back in again.
ILMV
I can solve this by holding backspace, but then it places a `:` at the very beginning of the value of which cannot be deleted. Also if I try selecting the entire value of the text box I get multiple `:` characters. Seems this solution has a little way to go yet.
ILMV
Finally the problem persists in FF, and if I type really fast I get `ab:1234:cd::`. I'm going to stop here, I don't know how difficult it would be to rectify these problems, this method would certainly be better than the masked edit solution if it was reliable. :)
ILMV