JSFiddle - React, Tailwind, and code Playground

by AJIEKCEU

HTML

<form action="">
  <div class="phone-number">
    <span>+7</span>
    <input class="phone-field phone-field_3" maxlength="3" type="tel">
    <span>-</span>
    <input class="phone-field phone-field_3" maxlength="3" type="tel">
    <span>-</span>
    <input class="phone-field phone-field_2" maxlength="2" type="tel">
    <span>-</span>
    <input class="phone-field phone-field_2" maxlength="2" type="tel">
  </div>
</form>

CSS

* {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

.phone-number{
  display: inline-block;
  vertical-align: middle;
  text-align: center;
}
.phone-number span{
  display: inline-block;
  vertical-align: middle;
}
.phone-field{
  display: inline-block;
  vertical-align: middle;
  width: 38px;
  height: 34px;
  padding: 0 1px;
  font-size: 14px;
  color: #555;
  text-align: center;
  background-color: #fff;
  background-image: none;
  border: 1px solid #ccc;
  border-radius: 4px;
    -webkit-box-shadow: 0 0 2px 2px rgba(0, 0, 0, .05);
    box-shadow: 0 0 2px 2px rgba(0, 0, 0, .05);
  -webkit-transition: border-color ease-in-out .15s,-webkit-box-shadow ease-in-out .15s;
  -o-transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
  transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
}
.phone-field:focus {
    border-color: #66afe9;
    outline: 0;
    -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075), 0 0 8px rgba(102,175,233,.6);
    box-shadow: inset 0 1px 1px rgba(0,0,0,.075), 0 0 8px rgba(102,175,233,.6);
}
.phone-field_2{
  width: 30px;
}

JavaScript

var body = $('body');
function checkingNumber(input) {
    input.value = input.value.replace(/[^\d]/g, '');
};

body.on('keyup', '.phone-field', function(){
  checkingNumber(this);
  var that = $(this),
  		maxL = that.attr('maxlength'),
  		q = that.val().length;
  
  if(q == maxL){
  	console.log(maxL +'-'+ q);
    that.next('span').next('.phone-field').focus();
  }
});