Created
February 19, 2022 20:07
-
-
Save Stringsaeed/cad5b38abdc34dc1a79aebf590dfa2d4 to your computer and use it in GitHub Desktop.
Phone validation using google lib phone 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
import {PhoneNumberUtil} from 'google-libphonenumber'; | |
export const isPhoneNumber = (number?: string) => { | |
try { | |
if (!number) { | |
throw new Error('Phone number is required'); | |
} | |
if (number.length < 4) { | |
throw new Error('Phone number is too short'); | |
} | |
const phoneUtil = PhoneNumberUtil.getInstance(); | |
const parsedNumber = phoneUtil.parse(number, 'EG'); | |
return phoneUtil.isValidNumberForRegion(parsedNumber, 'EG'); | |
} catch (e) { | |
if (__DEV__) { | |
console.log(e, number); | |
} | |
return false; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment