javascript - Mask if more then x characters -
i using masking plugin (http://digitalbush.com/projects/masked-input-plugin/) works, trying work once there 5+ characters in textbox. whenever hit 6th character, clears out textbox , won't accept input anymore.
js:
<script> function convert(){ if (l.length > 5){ $("#q").mask('(999) 999-9999'); } } </script>
html:
<form action="search.php" method="get"> <input type="tel" class="form-control" name="q" id="q" placeholder="phone/order number" onkeyup="convert()"><br> <button type="submit" class="btn btn-default">search</button> </form>
if please me, great. :d
it clears out textbox because when apply mask, plugin clears input. can around passing autoclear
option , set false. keep attempting apply mask, you'd have remove event listener. ditch inline event handler, that's bad way event handlers anyway :-)
something work:
$('#q').on('keyup', function() { var $el = $(this); var val = $el.val(); if (val.length > 5){ $el.mask('(999) 999-9999', { autoclear: false, placeholder: '' }); } });
note set placeholder
empty string, otherwise when mask applied cursor set end of mask placeholder , you'd have move caret few spaces.
Comments
Post a Comment