Javascript Dashes in Phone Number

Javascript dashes in phone number

First, clean your input by deleting all chars that are not numbers (ref.: Regex to replace everything except numbers and a decimal point)

Then, you put your dashes.

function addDashes(f)
{
f_val = f.value.replace(/\D[^\.]/g, "");
f.value = f_val.slice(0,3)+"-"+f_val.slice(3,6)+"-"+f_val.slice(6);
}

Automatically add hyphen in phone number e.g 111-111-1111

It's fairly messy, but you could use regex and replace in order to do it:

console.log('1111111111'.replace(/(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)/, '$1$2$3-$4$5$6-$7$8$9$10'))

Add dash in auto complete phone number

$('#phone-number-field').keyup(function(){
$(this).val($(this).val().replace(/(\d{3})\-?(\d{3})\-?(\d{4})/,'$1-$2-$3'))
});

Auto Dash using Javascript on FOCUS (for a telephone number format)

foo = foo.match(new RegExp('.{1,4}$|.{1,3}', 'g')).join("-");

How to format phone number while typing?

You can try the following way:

$('#phone').keyup(function(){  adddashes(this);});
function adddashes(el){ let val = $(el).val().split('-').join(''); //remove all dashes (-) if(val.length < 9){ let finalVal = val.match(/.{1,3}/g).join('-');//add dash (-) after every 3rd char. $(el).val(finalVal); }}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><input type="tel" class="form-control" id="phone" maxlength="12" required>

regex 13 digits phone number with dash

So 13 characters in total with a maximum of 3 dashes and a minimum of 1 means 10 digits right? Therefor your characters are ranging 11-13?

If so, try:

^(?=(?:\d-?){10}$)\d+(?:-\d+){1,3}

See an online demo

  • ^ - Start line anchor.
  • (?= - Open a positive lookahead:
    • (?: - Open a non-capture group:
      • \d-? - Match a digit and optional hyphen.
      • ){10}$) - Close the non-capture group and match it ten times before the end-string anchor. Then close the lookahead.
  • \d+ - 1+ Digits.
  • (?: - Open a 2nd non-capture group:
    • -\d+ - Match an hyphen and 1+ digits.
    • ){1,3} - Close non-capture group and match it 1-3 times.

JavaScript RegExp For Phone number validation that need to allowing hyphens and space in between the numbers

Try the following pattern:

^\d([0-9 -]{0,10}\d)?$

console.log(/^\d([0-9 -]{0,10}\d)?$/.test('123'));             // passconsole.log(/^\d([0-9 -]{0,10}\d)?$/.test('1-2 3'));           // passconsole.log(/^\d([0-9 -]{0,10}\d)?$/.test('1-2-3-4-5-6'));     // passconsole.log(/^\d([0-9 -]{0,10}\d)?$/.test('1-2-3-4-5-6-'));    // failconsole.log(/^\d([0-9 -]{0,10}\d)?$/.test('123456789012'));    // passconsole.log(/^\d([0-9 -]{0,10}\d)?$/.test('1234567890123'));   // failconsole.log(/^\d([0-9 -]{0,10}\d)?$/.test('- --'));            // fail

Regex for phone number being 10 digits and allowing "-" (Javascript)

Match every of the three parts, separated by the dashes, like this:

^0\d-\d{4}-\d{4}$

This matches 0x-xxxx-xxxx. Demo: https://regex101.com/r/nW7wL5/1

If you also want to match the number without the dashes, use

^0\d-?\d{4}-?\d{4}$

Demo: https://regex101.com/r/gY0mC3/1

\d is the same as [0-9] but it's shorter.



Related Topics



Leave a reply



Submit