I have an input field and the user can either type in a code or copy/paste it from an email. I need to check the length of the input val() to activate a button afterwards, if the given value has a specific length. The length check works fine while typing, but not every time when pasting a value. The length is mostly 0 then - but not EVERY time. What am I doing wrong here, I don't get it? It should work EVERY TIME.
$(document).on('keyup change paste click', '#promocode', function () {
var code = $(this).val();
$("#result").html(code.length);
if (code.length == 8) {
$("#result").html("length ok");
}
else {
$("#result").html("NOT ok");
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
Copy/Paste a value into this field:
<form>
<input type="text" id="promocode" placeholder="xxxx-xxx" />
<div id="result">Enter value (8-digits)</div>
</form>