You can use the jQuery plugin Mask.
It's pretty simple to use, just add how to you want to format your number:
$(document).ready(function(){
$('#your_id').mask('11-11-11-11-11');
});
jsFiddle with example.
Question
Is there any way to break user input? ex. A user will type its physical address in a textbox, when he enter his value I want to insert '-' character so that when user will type '002255667788' I can format it automatically '00-22-55-66-77-88'.
Can I do that in keypress?thank you.
Solution
You can use the jQuery plugin Mask.
It's pretty simple to use, just add how to you want to format your number:
$(document).ready(function(){
$('#your_id').mask('11-11-11-11-11');
});
jsFiddle with example.
OTHER TIPS
If you are using jQuery you can listen to the blur event and format the value of the input:
$('#your_input').on('blur', function(){
//clean the non numeric values
this.value = this.value.replace(/[^\d]/g,'')
//place the dashes
.replace(/(\d{2})/g,'$1-')
})
You can also change the value on key events, but be careful to detect when it's delete
, enter
, and so on..