material-ui-phone-number icon indicating copy to clipboard operation
material-ui-phone-number copied to clipboard

how to validate phone number based on country

Open Umarani-D opened this issue 4 years ago • 7 comments

Umarani-D avatar Jul 13 '20 12:07 Umarani-D

+1

imosapatryk avatar Oct 20 '20 20:10 imosapatryk

+1

raghvendra141990 avatar Jun 08 '21 16:06 raghvendra141990

+1

Begimai02 avatar Jun 27 '21 13:06 Begimai02

Use https://www.npmjs.com/package/phone

Woodylai24 avatar Aug 16 '21 04:08 Woodylai24

I use phoneUtils from 'google-libphonenumber'

Then you can do phoneUtil.isValidNumberForRegion(phoneUtil.parse(phone, 'CH'), 'CH') phoneUtil.isValidNumber(phoneUtil.parseAndKeepRawInput(phone))

david-vendel avatar Oct 13 '21 20:10 david-vendel

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.

HenriqueDerosa avatar Oct 19 '21 19:10 HenriqueDerosa

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) }

jmartinezgdev avatar Dec 22 '21 14:12 jmartinezgdev