There is a phone entry field. Dvizhek and nearby services want to use phones without +7 or 8

There is a usual input mask on this water field. http://digitalbush.com/projects/masked-input-plugin/ Mask is set

$("#telephone").mask("(999) 999-9999"); 

As a result, we see: (___) __-____

Juzveri often do not notice in the mask +7 and enter phones from 8

Question. How to track the first character entered and delete +7 or 8?

 $('.limitInput').keyup(function(){ var count = $(this).val().length; alert(count);}); 

does not work, since alert (count) will always return 14 characters (the mask has already been set).

    2 answers 2

    You can add " +7 " to the mask, and then cut these characters at the stage of processing the form (either on the client or on the server). The code itself may look like this:

     $("#telephone").mask("+7 (999) 999-9999"); $('#submit-btn').click(function() { var phone = $('#telephone').val().replace(/^\+7\s/, ''); // ... А здесь должен быть код, использующий телефон в нужном формате }); 

    JSFiddle: http://jsfiddle.net/p5edtogz/1/

      In http://digitalbush.com/projects/masked-input-plugin/ you can create your own mask. If you want the first character not to be +7 or 8, then add to the code:

       $.mask.definitions['~']='[01234569]'; // Первая цифра не может быть 7 или 8. $("#telephone").mask("(~99) 999-9999");