Skip to content

Instantly share code, notes, and snippets.

@5iDS
Created February 28, 2016 21:49
Show Gist options
  • Save 5iDS/598e523b98340183be21 to your computer and use it in GitHub Desktop.
Save 5iDS/598e523b98340183be21 to your computer and use it in GitHub Desktop.
Matches South African telephone/mobile numbers, with or without the country code. Can also include spaces or hyphens between values. Doesn't match brackets around the area code
/**
* Matches: 0111231234 | 011 123 1234 | 011-123-1234 | 0821231234 | +27821231234 | +2782-123-1234 | +2782 123 1234 | 27111231234 | 2711 123 1234 | 2711-123-1234
* Non-Matches: (011)1231234 | (+2711) 123 1234 | (011) 123-1234
/**/
var pattern2 = new RegExp(/^((?:\+27|27)|0)(\d{2})-?(\d{3})-?(\d{4})$/);
console.log( pattern2.test( '+27134622241' ) );
@tarun-nagpal-github
Copy link

It fails in one condition.

var pattern2 = new RegExp(/^((?:+27|27)|0)(\d{2})-?(\d{3})-?(\d{4})$/);
console.log( pattern2.test( '0000000000' ) );

Can you please improve it ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment