Skip to content

Instantly share code, notes, and snippets.

@PeterKow
Created April 23, 2019 10:12
Show Gist options
  • Select an option

  • Save PeterKow/6982d9f3119855652699a9b04895422b to your computer and use it in GitHub Desktop.

Select an option

Save PeterKow/6982d9f3119855652699a9b04895422b to your computer and use it in GitHub Desktop.
Validate UK number
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