Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate phone number with Yup?

Tags:

javascript

yup

I'm trying to validate a phone number with Yup:

phone: Yup.number()   .typeError("That doesn't look like a phone number")   .positive("A phone number can't start with a minus")   .integer("A phone number can't include a decimal point")   .min(8)   .required('A phone number is required'), 

.min(8) validates that the number is 8 or more. So simply entering 8 will pass. How can I make 8 characters required so 1000 0000 would pass?

like image 858
Evanss Avatar asked Sep 24 '18 16:09

Evanss


People also ask

How do I validate only numbers in Yup?

shape({ phone: yup . string() // regexr.com/6anqd . matches(/(\+91\ )[6-9]{1}[0-9 ]{4}[0-9 ]{4}[0-9]{3}/, { message: "Invalid Indian number", excludeEmptyString: false, }) . required(), email: yup.

Is there a free way to verify a phone number?

1. Google. This is the brute force method of identifying a phone number, but it's quick, easy, and completely free. If the call is from an official or otherwise public source, a search engine like Google or DuckDuckGo might come up with every last detail about the location and owner of the phone number.


1 Answers

Hi right now I'am solving same problem as you and I found possible solution.

Validate phone number with string that matches Regex

const phoneRegExp = /^((\\+[1-9]{1,4}[ \\-]*)|(\\([0-9]{2,3}\\)[ \\-]*)|([0-9]{2,4})[ \\-]*)*?[0-9]{3,4}?[ \\-]*[0-9]{3,4}?$/  phoneNumber: Yup.string().matches(phoneRegExp, 'Phone number is not valid') 

You can search for different Regex Expressions and validate it. I've used Regex from this article https://www.sitepoint.com/community/t/phone-number-regular-expression-validation/2204

like image 56
filippofilip Avatar answered Sep 22 '22 10:09

filippofilip