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 8851881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:15:55+00:00 2026-06-14T13:15:55+00:00

I currently use the script below to take any form of information and pull

  • 0

I currently use the script below to take any form of information and pull out the numbers to make a phone number with dashes in it. However, if I accidently click in the field, it puts two dashes in there even though nothing was pasted or typed. Does JS have a way to say ONLY if something is pasted then add dashes? The reason it’s a pain is I have 2 search fields, and if I want to use one, the other has to be blank. So if there are 2 dashes in it, I have to delete them out and hit enter in the same field or it will add them again.

I appreciate any help you might have.

<SCRIPT LANGUAGE="JavaScript">
    function addDashes(f)
    {
        f.value = f.value.replace(/\D/g, '');
        f.value = f.value.slice(0,3)+"-"+f.value.slice(3,6)+"-"+f.value.slice(6,15);
    }
</SCRIPT>
  • 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-06-14T13:15:56+00:00Added an answer on June 14, 2026 at 1:15 pm

    I’m not quite sure if this is what you need, but this just checks to see if there is actually something there before formatting it:

    function addDashes(f)
    {
        var oldVal = f.value.replace(/\D/g, '');
        if (oldVal.length) {
            f.value = oldVal.slice(0,3) + "-" + oldVal.slice(3,6) + "-" + oldVal.slice(6,15);
        }
    }
    

    EDIT:

    Based on your comment, I thought it might be helpful to bring up validation. I’m not sure if you are doing anything on the server-side to make sure it is a valid phone number, but it might be helpful to do a little validation so taht you don’t add dashes if the user has just typed some spaces.

    First, I would remove the non-numeric values before you check the length. I’ve updated the code above to do that.

    Next, I would check against some length. Maybe you want to only add dashes if the number is at least 9 digits long. You can decide that length taht you watn to check against. In that case, you would add:

    if (oldVal.length >= 9) { ...
    

    It all depends on if/how you are validating this field.

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

Sidebar

Related Questions

Currently we use separate a drop statements for each stored procedure in the script
I use the below script to allow visitors to my page to increase /
I'm currently using the below script which I found online: ######################################### #### BEGIN EDIT
I'm currently trying to use a script that ensures users are logged into a
I decided to use the JQuery UI Datepicker script for picking dates. Below is
I currently use Zend_Db to manage my queries $stmt = $db->prepare(INSERT INTO test (ID_Test)
I currently use Devise 2.1 + Rails 3.2.x to authenticate users. I'm also going
I currently use the rectangle shape xml tag to specify borders for my views
I currently use a combination of LVL and Proguard as a first line of
I currently use mysql_real_escape_string to escape a variable when querying the database to prevent

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.