Skip to content

Instantly share code, notes, and snippets.

@amhinson
Last active November 28, 2018 17:18
Show Gist options
  • Save amhinson/79799400801f0623c6f9fe19bdb3dc21 to your computer and use it in GitHub Desktop.
Save amhinson/79799400801f0623c6f9fe19bdb3dc21 to your computer and use it in GitHub Desktop.
Yup Phone Validation
import libphonenumber from 'google-libphonenumber';
const phoneUtil = libphonenumber.PhoneNumberUtil.getInstance();
export const validPasswordRegex = /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$/;
export const phoneValidation = function(message: string) {
return this.test({
name: 'phone',
exclusive: true,
message,
test: value => {
try {
const phone = phoneUtil.parse(value, 'US');
return phoneUtil.isPossibleNumber(phone);
} catch (e) {
return false;
}
}
});
};
// Usage
yup.addMethod(yup.mixed, 'phone', phoneValidation);
yup.object().shape({
phone: yup
.string()
.phone('Must use a valid phone number')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment