#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2004
    Location
    Boston, MA USA
    Posts
    567
    Rep Power
    51

    Auto-format phone number field


    This may be a javascript issue, I am not sure.

    Does anyone know of a good way to auto-format an input form field for a telephone number so that it automatically formats the entry with "()" and "-" when the user simply types numerical characters?

    i.e. as the user types "1234567890", the form field resolves it as "(123)456-7890"

    Any thoughts appreciated
  2. #2
  3. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,869
    Rep Power
    9646
    The general concept is often called "masking" and it requires Javascript - PHP can't do it.
  4. #3
  5. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,124
    Rep Power
    4309
    If you use jQuery, I recommend this mask plugin.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    238
    Rep Power
    64
    And maybe a little regex to ferret out totally bad numbers. Also, I often strip non-numerical before formatting (naturally, of course) - But it wouldn't surprise me if jQuery does it all. I use a JS form validation class that takes care of things, garbage and formatting and such. You never know what crap a user will put in the box.

IMN logo majestic logo threadwatch logo seochat tools logo