Search code examples
javascriptjquerykeypresskeyup

Break user input in jquery/javascript


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.