Javascript dashes in phone number
<p>I tried to research the answer to this question but I'm lost. I am trying to make a one search bar that automatically puts a dash in the phone number. I've solved that.</p>

<p>The next part is the challenging part. How can I make it always do XXX-XXX-XXXX, even if the characters pasted were something like 555 555 1212 or 555---555-1212, where it will only reel back the number and output with 555-555-1212. It shouldn't count the spaces or extra dashes as a character. </p>

<p>I found: <a href="http://www.jotform.com/answers/15202-can-I-add-script-to-my-form-that-will-automatically-add-hyphens-in-between-the-3-digit-area-code-and-also-the-3-digit-prefix" rel="nofollow">http://www.jotform.com/answers/15202-can-I-add-script-to-my-form-that-will-automatically-add-hyphens-in-between-the-3-digit-area-code-and-also-the-3-digit-prefix</a> </p>

<p>I changed it just a bit by adding:</p>

<pre><code>&lt;SCRIPT LANGUAGE="JavaScript"&gt;
        function addDashes(f)
            {
                f.value = f.value.slice(0,3)+"-"+f.value.slice(3,6)+"-"+f.value.slice(6,15);
            }
    &lt;/SCRIPT&gt;


&lt;input id="input_4" class="form-textbox" maxlength="15" name="atn" size="25" onBlur='addDashes(this)' /&gt;
</code></pre>

<p>Right now, this works only if the user puts 5555555555 and automatically turns it into 555-555-5555. I'm trying to figure out how to take something like 5-55555-5555 and turn it into 555-555-5555. Currently, it makes it 5-5-555-5-5555.</p>

<p>See my dilemma? lol. It can't be php or any server side scripting as this must be able to run on a desktop. </p>

<hr>

<p>Resolution:</p>

<pre><code>&lt;SCRIPT LANGUAGE="JavaScript"&gt;
        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);
            }
    &lt;/SCRIPT&gt;
</code></pre>
