Pure java script code for Validating US phone Number with a little knowledge of regular expression


let myregx2=new RegExp('[0-9()]',"g");
let arrWhenLength12=[0,4];
let arrWhenLength13=[1,5];
function telephoneCheck(str) {
    let mymainstr=str.match(myregx2);
  let isfirstnumregx=new RegExp('^[0-9]','g');
  if(isfirstnumregx.test(str)){
    let a=false
  mymainstr.length>=14?a=false
  :mymainstr.length===13&&parseInt(mymainstr[0])===1&&brktbool(mymainstr.join(""))?
  a=true:mymainstr.length===12&&brktbool(mymainstr.join(""))?
  a=true:mymainstr.length===11&&parseInt(mymainstr[0])===1?
  a=true:mymainstr.length===10?a=true:mymainstr.length<=9?a:undefined;
    return a;
  }if(mymainstr.length===12&&brktbool(mymainstr.join(""))){return true}
  else{
    return false;
  }
}
function brktPosition(arr){
  let need=[];
  for(let i=0;i<arr.length;i++){
   if(arr[i]==="("||arr[i]===")"){
     need.push(arr.indexOf(arr[i]));
  }
  }
  return need;
  }
  function brktbool(arr){
    if(arr.length===12)
   return brktPosition(arr).join("")===arrWhenLength12.join("")
    if(arr.length===13)
    {
   return brktPosition(arr).join("")===arrWhenLength13.join("")
   }
  }

console.log(telephoneCheck("(555)555-5555"));

Yeah, that little knowledge is what’s causing all that imperative code that I can’t quite grasp what it’s doing :frowning:

What I recommend is to review this video:

https://www.youtube.com/watch?v=EkluES9Rvak

And then use this website: https://regex101.com/ to test your own regex (or attempts) with a given set of test inputs until you get it right.

The key things to look for in Regex for this case is the \d and escaped characters \( and \) because the parenthesis without being escaped mean “capturing groups.” the dash is also a reserved character but IDK if you have to escape it outside a character range [] also you may want to look into the + symbol and the ‘match range indicator’ {from,to}

1 Like
let arrWhenLength12=[0,4];
let arrWhenLength13=[1,5];

the above code is test if the braces “(” and “)” index position when string length is 12 or 13

function brktPosition(arr){
  let need=[];
  for(let i=0;i<arr.length;i++){
   if(arr[i]==="("||arr[i]===")"){
     need.push(arr.indexOf(arr[i]));
  }
  }
  return need;
  }

the above code return index of braces

function brktbool(arr){
    if(arr.length===12)
   return brktPosition(arr).join("")===arrWhenLength12.join("")
    if(arr.length===13)
    {
   return brktPosition(arr).join("")===arrWhenLength13.join("")
   }
  }

and this function return Boolean comparing given arrays and return of brktPosition function
the rest i use ternary operator to check all possible combination of valid phone number which are(13,12,11,10) for example if phone number length is 13 braces index position must be 1 and 5 and first number must be 1 for length 12 braces index position is 0,4 and for length 11 first index must be 1…hope i make it clear as much as i can

Thank you bro!! for me the as a beginner long regex expression is confusing for me for now i will dig regx but the above code will pass any format based on FCC Telephone Number Validator rules.

@chafchaff

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.

We have set your post to unlisted. Thanks for your understanding.

I understand.Thank you very much for the tip.