Last active
November 28, 2018 17:18
-
-
Save amhinson/79799400801f0623c6f9fe19bdb3dc21 to your computer and use it in GitHub Desktop.
Yup Phone Validation
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 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