Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6644979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:13:50+00:00 2026-05-26T00:13:50+00:00

How can I mask a US phone using javascript on an HTML control. I

  • 0

How can I mask a US phone using javascript on an HTML control.

I would like the input to force the text to be in the following format:

[(111)111-1111]

Here is what I have currently:

mask(str, textbox, loc, delim) {
    var locs = loc.split(',');
    for (var i = 0; i <= locs.length; i++) {
        for (var k = 0; k <= str.length; k++)
        {
            if (k == locs[i]) {
                if (str.substring(k, k + 1) != delim) {
                    str = str.substring(0,k) + delim + str.substring(k,str.length)
                }
             }
         }
     }
     textbox.value = str
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:13:51+00:00Added an answer on May 26, 2026 at 12:13 am

    There are three common ways for handling phone number input of a guaranteed format:

    1. Accept all numerical input anyway

    The likelihood of users with obscure numbers but still living in the US is higher than ever. Accepting all kinds of numerical input alleviates the concern as the number will only be useless if they gave you either not enough numbers or the wrong ones.

    2. Split it into three text boxes of fixed lengths

    A lot of financial software does this. Not sure why, specifically, but it seems to be rather frequent there. A recommendation is to advance the cursor after keypress to the next box if they’ve typed the max limit on the textboxes. Also, this guarantees you will get the numbers in whatever format you’re expecting, because you can just append the resulting post variables together.

    Example of the HTML:

    <input id="phonePart1" maxlength="3" name="phonePart1"/>
    <input id="phonePart2" maxlength="3" name="phonePart2"/>
    <input id="phonePart3" maxlength="4" name="phonePart3"/>
    

    and a little jQuery snippet to merge the three in your format:

    var phonePart1 = parseInt($("#phonePart1").val(), 10);
    var phonePart2 = parseInt($("#phonePart2").val(), 10);
    var phonePart3 = parseInt($("#phonePart3").val(), 10);
    var phone = "(";
    
    if (isNaN(phonePart1)||isNaN(phonePart2)||isNan(phonePart3)) {
    
        // Incorrect format
    
    } else { 
    
        phone = phone + phonePart1 + ")" + phonePart2 + "-" + phonePart3;
    
    }
    

    3. Use a regular expression to match the number format

    You can use a combination of regular expressions to match multiple numbers, or explicitly the one you are asking about. This is probably the technique you’re looking for.
    This is the regular expression:

    /\([0-9]{3}\)[0-9]{3}-[0-9]{4}/
    

    You’d use it like this:

    if (yourphonevariable.match(/\([0-9]{3}\)[0-9]{3}-[0-9]{4}/))
    {
        // it's valid
    }
    

    4. If you’re looking to format the text itself with a mask…

    Consider the jQuery plugin at http://digitalbush.com/projects/masked-input-plugin/. User @John Gietzen suggested this to me outside of this post, so feel free to give him kudos for it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I mask input from the command line? Basic use case: I want
How can I mask my flash movie dynamically using actionscript to create a 5
How can create a mask input for number that have percent by jQuery? Do
I can create an opacity mask in WPF using a LinearGradientBrush as follows <LinearGradientBrush
How can I control to opacity of a disabled panel with a mask. I
Can anyone parse this following expression for me #define Mask(x) (*((int *)&(x))) I applied
I would like to create a modal window which will only mask the center
For north american phone numbers, (999) 999-9999 works pretty well for an input mask.
I realise you can make dimentions fluid by using the autoresizing mask to give
I have a text box with a mask that requires 4 digits. How can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.