Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><a href="https://i.stack.imgur.com/2UrVV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2UrVV.png" alt="enter image description here"></a>This solution <em>actually validates the numbers</em> and the format. For example: <strong>123-456-7890 is a valid format but is NOT a valid US number</strong> and this answer bears that out where others here do not. </p> <hr> <p>If you do not want the extension capability remove the following including the parenthesis: (?:\s*(?:#|x.?|ext.?|extension)\s*(\d+)\s*)? :) </p> <p><strong>edit</strong> <em>(addendum)</em> I needed this in a client side only application so I converted it. Here it is for the javascript folks:</p> <pre><code>var myPhoneRegex = /(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]‌​)\s*)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)([2-9]1[02-9]‌​|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})\s*(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+)\s*)?$/i; if (myPhoneRegex.test(phoneVar)) { // Successful match } else { // Match attempt failed } </code></pre> <p>hth. <strong>end edit</strong> </p> <p>This allows extensions or not and works with .NET</p> <pre><code>(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]‌​)\s*)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)([2-9]1[02-9]‌​|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$ </code></pre> <p>To validate with or without trailing spaces. Perhaps when using .NET validators and trimming server side use this slightly different regex:</p> <pre><code>(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]‌​)\s*)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)([2-9]1[02-9]‌​|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})\s*(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+)\s*)?$ </code></pre> <p>All valid:</p> <p>1 800 5551212</p> <p>800 555 1212 </p> <p>8005551212</p> <p>18005551212</p> <p>+1800 555 1212 extension65432</p> <p>800 5551212 ext3333</p> <p>Invalid #s</p> <p>234-911-5678</p> <p>314-159-2653</p> <p>123-234-5678 </p> <hr> <blockquote> <p><strong>EDIT:</strong> Based on Felipe's comment I have updated this for international.</p> </blockquote> <p>Based on what I could find out from <a href="https://en.wikipedia.org/wiki/E.164" rel="nofollow noreferrer">here</a> and <a href="https://en.wikipedia.org/wiki/List_of_country_calling_codes#Locations_with_no_country_code" rel="nofollow noreferrer">here</a> regarding valid global numbers </p> <p>This is tested as a first line of defense of course. An overarching element of the international number is that it is no longer than 15 characters. I did not write a replace for all the non digits and sum the result. It should be done for completeness. Also, you may notice that <strong>I have not combined the North America regex with this one.</strong> The reason is that this international regex will match North American numbers, however, it will also accept known invalid <code>#</code> such as <code>+1 234-911-5678</code>. For more accurate results you should separate them as well. </p> <p>Pauses and other dialing instruments are not mentioned and therefore invalid per E.164 </p> <pre><code>\(?\+[0-9]{1,3}\)? ?-?[0-9]{1,3} ?-?[0-9]{3,5} ?-?[0-9]{4}( ?-?[0-9]{3})? </code></pre> <p>With 1-10 letter word for extension and 1-6 digit extension:</p> <pre><code>\(?\+[0-9]{1,3}\)? ?-?[0-9]{1,3} ?-?[0-9]{3,5} ?-?[0-9]{4}( ?-?[0-9]{3})? ?(\w{1,10}\s?\d{1,6})? </code></pre> <p>Valid International: Country name for ref its not a match. </p> <p>+55 11 99999-5555 <strong>Brazil</strong></p> <p>+593 7 282-3889 <strong>Ecuador</strong></p> <p>(+44) 0848 9123 456 <strong>UK</strong></p> <p>+1 284 852 5500 <strong>BVI</strong></p> <p>+1 345 9490088 <strong>Grand Cayman</strong></p> <p>+32 2 702-9200 <strong>Belgium</strong></p> <p>+65 6511 9266 <strong>Asia Pacific</strong></p> <p>+86 21 2230 1000 <strong>Shanghai</strong></p> <p>+9124 4723300 <strong>India</strong></p> <p>+821012345678 <strong>South Korea</strong></p> <p><em>And for your extension pleasure</em> </p> <p>+55 11 99999-5555 ramal 123 <strong>Brazil</strong></p> <p>+55 11 99999-5555 foo786544 <strong>Brazil</strong></p> <p>Enjoy</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload