Created
April 23, 2019 10:12
-
-
Save PeterKow/6982d9f3119855652699a9b04895422b to your computer and use it in GitHub Desktop.
Validate UK number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function isValidUKPhone(phone: string) { | |
| const re = /^(\+44\d{10}\b)/; | |
| return re.test(phone); | |
| } | |
| describe('isValidUKPhone', () => { | |
| it('should return true as valid number', () => { | |
| const phone = '+447473222885'; | |
| expect(isValidUKPhone(phone)).toBeTruthy(); | |
| }); | |
| it('should return false for too long number', () => { | |
| const phone = '+4474732228852'; | |
| expect(isValidUKPhone(phone)).toBeFalsy(); | |
| }); | |
| it('should return false for other prefix than +44', () => { | |
| const phone = '+407473222885'; | |
| expect(isValidUKPhone(phone)).toBeFalsy(); | |
| }); | |
| it('should return false for missing +', () => { | |
| const phone = '447473222885'; | |
| expect(isValidUKPhone(phone)).toBeFalsy(); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment