material-ui-phone-number
material-ui-phone-number copied to clipboard
how to validate phone number based on country
+1
+1
+1
Use https://www.npmjs.com/package/phone
I use phoneUtils from 'google-libphonenumber'
Then you can do
phoneUtil.isValidNumberForRegion(phoneUtil.parse(phone, 'CH'), 'CH')
phoneUtil.isValidNumber(phoneUtil.parseAndKeepRawInput(phone))
Great @Woodylai24 , it worked really well. It validates for every country. This is what I did, using React Hook Forms
rules={{
validate: value => phone(value, {
validateMobilePrefix: false,
})?.isValid
}}
Thanks!
Edit: It already identifies the country if your value
has +
sign as first character.
How can we access to object country data? In the original lib we can do it from onChange callback
country data | object | the country object { name, dialCode, country code (iso2 format) } |
---|