This is a verification code form, and I want to show entered number on yellow input, but the problem is when you enter 4 numbers then enter new numbers or update old numbers yellow input not updated, and I want to show number on yellow input only if all 4 inputs are has value, if not, clear yellow input.
var code = [];
$('.verf-code input').keyup(function(e) {
var key = e.which;
var val = $(this).val();
$(this).each(function() {
if (key >= 48 && key <= 57) {
$(this).next('input').select().focus();
code.push(val);
return true;
} else {
return false;
}
});
if (code.length <= 4) {
$('.verification-code').val(code.join(''));
} else {
code = [];
}
});
.verification-code {
background: yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="verf-code"><input class="afc-form__item__input" name="verf" type="text" maxlength="1" tabindex="1" />
<input class="afc-form__item__input" name="verf" type="text" maxlength="1" tabindex="2" />
<input class="afc-form__item__input" name="verf" type="text" maxlength="1" tabindex="3" />
<input class="afc-form__item__input" name="verf" type="text" maxlength="1" tabindex="4" /></div>
<input type="text" class="verification-code">