jQuery prepopulate and format a text field -
this confusing me.
i have php app text field, client wants field prepopulated 896- , followed user input of 5 numeric values, 896-12569. i
i in jquery, possible, if how?
i'd suggest doing using css, here jquery solution:
$('input').on('keyup', function() { var prefix = '896-'; var value = this.value.replace(prefix, '').replace(/[^\d]/, '').substr(0, 5); this.value = prefix + value; }).keyup(); // trigger keyup event, field populated @ load
obviously swap input
relevant selector, , add events necessary (this example works on keyup
event).
edit
having looked @ code again, noticed there's small bug (press backspace once), ths prefix
gets added again (this because isn't removed input
because no longer matches), quick fix replace first 4 characters of input:
var value = this.value.replace(this.value.substr(0, 4), '').replace(/[^\d]/, '').substr(0, 5);
Comments
Post a Comment