phppup Posted June 1, 2019 Share Posted June 1, 2019 (edited) I want to use a particular input for telephone numbers. I have seen working examples, and either need to edit or create code for my end result Aside from the usual telephone number mask, this input field dynamically replaces the value/placeholder that is visible. To elaborate, the user sees the input text box with (___)___-____ displayed. As digits are entered, the field replaces underscores with digits in a progression until complete. (21_)___-____ , next (212)55_-____, then (212)555-12__ until finally (212)555-1212 Any direction would be helpful. Edited June 1, 2019 by phppup Quote Link to comment Share on other sites More sharing options...
Barand Posted June 2, 2019 Share Posted June 2, 2019 Something like this? <input type="text" name="phone" id="phone" value="(___) ___ - ____" > and $("#phone").keydown( function(e) { var output = $("#phone").val() if (output == '') output = "(___) ___ - ____" if (output.indexOf('_') != -1) { e.preventDefault() if (48 <= e.keyCode && e.keyCode <= 57) { // keyboard top row output = output.replace('_', e.keyCode-48) } else if (96 <= e.keyCode && e.keyCode <= 105) { // numeric keypad output = output.replace('_', e.keyCode-96) } $("#phone").val(output) } }) Quote Link to comment Share on other sites More sharing options...
phppup Posted June 3, 2019 Author Share Posted June 3, 2019 Something like that. Thanks for the starting point. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.