3

I am trying to create a function that would remove certain characters from a phone number field. So far I have removed parenthesis, spaces, and hyphens as in (321) 321-4321. Also, I removed dots as in 321.321.4321. Lastly, I have validated when 11 digits and starting with 1 even with punctuation.

The code I input:

function phoneNumber(){
   var number = '(321) 321-4321';
   number = number.replace(/[^\d]/g, '');
   return number;
}

I am now trying to have it return null for certain situations like:

  • When the number exceeds 11 digits
  • When letters and/or punctuation are present
  • When the area code does not start with 2-9
  • When the exchange code does not start with 2-9.

Any attempt to add a statement invalidates my original function. Can anyone help me I am new to regex, but I need help?

4
  • check this tutorial out when you can github.com/zeeshanu/learn-regex Commented Dec 19, 2017 at 16:09
  • Sidenote [^\d] is the same as \D Commented Dec 19, 2017 at 16:09
  • After the .replace why not just examine number? You know what you have is numeric, check its length and the leading characters. Commented Dec 19, 2017 at 16:10
  • When you say you want it to return null when letters or punctuation are present, what exactly do you mean? You existing regex is removing those letters, do you mean that you want to rewrite your expression such that if \D matches anything it fails? Commented Dec 19, 2017 at 16:13

2 Answers 2

2

Brief

If I understand correctly, you want:

  • To remove any non-digit characters from the string
  • To get numbers that are 11 digits in length (or less)
  • To ensure the first and fourth digits in the number are in the range 2-9 (so not 0 or 1)

Code

The first regex can use \D instead of [^\d] as they both mean the exact same thing.

The second part can use any of the following methods (and more, but these are probably some of the simplest methods):

Regex and .length

/^[2-9]\d{2}[2-9]/.test(number) && number.length <= 11

var numbers = [
  '(321) 321-4321', 
  '321.321.4321', 
  '123.456.7890',
  '321.123.4567',
  '(321) 321-4321-321'
];

function phoneNumber(number){
   number = number.replace(/\D/g, '');
   if(/^[2-9]\d{2}[2-9]/.test(number) && number.length <= 11) {
     return number;
   }
   return null;
}

numbers.forEach(function(number){
  console.log(phoneNumber(number));
});

charAt, regex and length

!/[01]/.test(number.charAt(0)) && !/[01]/.test(number.charAt(3)) && number.length <= 11

var numbers = [
  '(321) 321-4321', 
  '321.321.4321', 
  '123.456.7890',
  '321.123.4567',
  '(321) 321-4321-321'
];

function phoneNumber(number){
   number = number.replace(/\D/g, '');
   if(!/[01]/.test(number.charAt(0)) && !/[01]/.test(number.charAt(3)) && number.length <= 11) {
     return number;
   }
   return null;
}

numbers.forEach(function(number){
  console.log(phoneNumber(number));
});

Regex (alone)

Obviously, you'd change 0 to whatever your minimum digits requirements are (minus 4); so if you want a minimum of 9, you'd put {5,7}.

/^[2-9]\d\d[2-9]\d{0,7}$/.test(number)

var numbers = [
  '(321) 321-4321', 
  '321.321.4321', 
  '123.456.7890',
  '321.123.4567',
  '(321) 321-4321-321'
];

function phoneNumber(number){
   number = number.replace(/\D/g, '');
   if(/^[2-9]\d\d[2-9]\d{0,7}$/.test(number)) {
     return number;
   }
   return null;
}

numbers.forEach(function(number){
  console.log(phoneNumber(number));
});

Sign up to request clarification or add additional context in comments.

Comments

1

Try as follows use .length to get characters in string

var number = '(321) 321-4321';
number = number.replace(/[^\d]/g, '');
console.log(number)

if (number.length > 11) {
  console.log("null");
} else {
  console.log(number.length);
}


if (number.match(/[a-z]/i)) {
  console.log("alphabet letters found");
} else {
  console.log("alphabet letters not found");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.