This question already has answers here:
A comprehensive regex for phone number validation
                                
                                    (41个回答)
                                
                        
                                5年前关闭。
            
                    
嗨,我需要为电话号码建立一个正则表达式,例如:


  +79261234567
  +7 926 123 45 67
  89261234567
  79261234567
  8(926)123-45-67
  9261234567
  79261234567
  89261234567
  8-926-123-45-67
  8927 1234 234
  8927 12 12 888
  8927 12 555 12
  8927123 8123


我现在来到这个正则表达式:

/((8|\+7)[\- ]?)((\(?9\d{2}\)?[\- ]?)[\d\- ]{7,10})?[\d\- ]{10,10}/g

link to regexper.com

但是它无法正常工作,因此任何帮助将不胜感激

最佳答案

您可以尝试:

var input  = 'phone number',
    output = input.replace(/\(([^)]+)\)/, '$1').replace(/(^\+)|[- ]+/g, ''),
    result = /^\d{10,11}$/.test(output);


说明:

\(([^)]+)\) - looks for digits with brackets around them to be removed
(^\+)|[- ]+ - looks for starting `+` or whitespace/dash in number to be removed
^\d{10,11}$ - checks if number has exacly 10 to 11 digits

09-20 04:16