Good day!

There is an input field:

<input type="text" value="Введите номер" /> 

Necessary to do the following:

They entered this way 79051234567 to +7 (905) 123-45-67 You can enter as you like, as a result you should receive a formatted number - +7 (905) 123-45-67

  • one
    Just read the characters and insert the necessary punctuation marks after certain ones - Deonis
  • one
    You can still try digitalbush.com/projects/masked-input-plugin - flexible settings, everything, but this is the input mask. - RubaXa

2 answers 2

The plug-in, in this case, most likely, will be better, but I decided to indulge and dash off a primitive example . If you want, you can take as a basis.

     $(function(){ $('input').focusout(function(){ var numb = $(this).val(); var aNumb = numb.match(/\d/g); if (aNumb && aNumb.length==11){ aNumb = aNumb.join(''); var res = aNumb.replace(/^(\d)(\d{3})(\d{3})(\d{2})(\d{2})$/g,'+$1 ($2) $3-$4-$5'); $(this).val(res); } }); });