JavaScript Algorithms and Data Structures Projects: Telephone Number Validator

Tell us what’s happening:
how may I separate in groups to filter them better? and How may I improve my filter to get the correct codes.

Your code so far


function telephoneCheck(str) {
 let b=/\s+/gi;
 let c=/\W+/gi;
 let a=str.replace(b,"");

 if(str[0]=="("||str[0]==1||str[0]==5){
              
          let d=a.replace(c,"")
              console.log(d)    
           if(d.length==10){

                return true            
           }      
          
  }else{
   return false
 }
}


telephoneCheck("555-555-5555");


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36.

Challenge: Telephone Number Validator

Link to the challenge:

The easiest solution I can think of is just to match correct pattern from the start and until the end with regex. The way you’re trying to solve it would cause problems and be somewhat ugly. Try to refresh your regex skills and come back to the challenge later

1 Like

I am trying to make a regex, which can only accept at the beginning 1, 555 or both. Then, 7 numbers more. What can it try to make that with regex?

function telephoneCheck(str) {

let regex=/^1*|555*|[1-9]/gi

      let a=regex.test(str);

      if(a==true){

          console.log(a)

          return true;

      }else{

        return false;

      }

          
  

}

telephoneCheck("5555555555");

I have only this left: telephoneCheck("(555-555-5555") should return false. What may I do? and How can I make to get a better code. I know this is hard code.

function telephoneCheck(str) {
  
 let regex=/1 456 789 4444|1 456 789 4444|1 555 555 5555|555-555-5555|555555-5555|555 555-5555|555 555 5555|5555555555|1 555 555 5555/gim

  let a =   regex.test(str)
           
            if(   a ==  true ){

                   return true;
            
            } else if(    str   ==    "1 (555) 555-5555"    |   str   ==    "(555)555-5555"    |  str   ==   "1(555)555-5555")   {

                   return true;
            }else{
                    
                    return false;

            }
}

telephoneCheck("555-555-5555");