Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate phone number using Yup but is not required?

I am able to validate if what is typed a phone number using this regexp but it is requiring a phone number to be entered in the input field. I am trying to make phone number optional. When I remove .matches phoneRegExp, it works without being required. I believe it has something to do with the regexp that makes the field turn into required. But I am not sure. Should I do it a different way? thanks


        const phoneRegExp = /^((\\+[1-9]{1,4}[ \\-]*)|(\\([0-9]{2,3}\\)[ \\-]*)|([0-9]{2,4})[ \\-]*)*?[0-9]{3,4}?[ \\-]*[0-9]{3,4}?$/;

const signUpSchema = yup.object().shape({
  username: yup
    .string()
    .min(4, "Need at least 4 characters.")
    .required("Username is required"),
  firstName: yup
    .string()
    .min(2, "Need at least 2 characters.")
    .required("First name is required"),
  lastName: yup
    .string()
    .min(2, "Need at least 2 characters")
    .required("Last name is required"),
  email: yup
    .string()
    .email()
    .required("Email is required"),
  phoneNumber: yup.string()
    .matches(phoneRegExp, "Phone number is not valid")
    .notRequired(),
  password: yup
    .string()
    .min(6, "Password must be at least 6 characters.")
    .required("Password is required"),
  instructorOrClient: yup
    .string()
    .matches(/(instructor|client)/, "Either instructor or client.")
    .required("Please select instructor or client."),
});
like image 553
Random Sog Avatar asked Apr 28 '20 01:04

Random Sog


People also ask

How can I validate my mobile number 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.

What is phone no validation?

Phone number validation is the process of checking if a phone number is accurate. It lets you find out if the phone number you have for a business contact or customer is active and able to receive calls.

How do I validate a phone number in Yup?

When you call phoneschema validateSync in your submit handler, Yup will validate the schema and raise errors, which you can catch and display to your user. Another way of validating a phone number is to use a dedicated phone number validation API like AbstractAPI’s Free Phone Number Validation API.

Do you have validation to check the phone number starting with 9?

Yes. do you have validation to check wheter the phone num starting with 9, 8,7 and also to check it contain 10 digit using html and java script Thank you Mukesh, I was getting lot of spam sign ups due to no validation in mobile number.

How to validate a phone number with 10 digits?

At first, we validate a phone number of 10 digits with no comma, no spaces, no punctuation and there will be no + sign in front the number. Simply the validation will remove all non-digits and permit only phone numbers with 10 digits. Here is the function.

How to validate a phone number in different format using JavaScript?

In this page we have discussed how to validate a phone number (in different format) using JavaScript : At first, we validate a phone number of 10 digits with no comma, no spaces, no punctuation and there will be no + sign in front the number. Simply the validation will remove all non-digits and permit only phone numbers with 10 digits.


Video Answer


1 Answers

Add excludeEmptyString:true to matches. need to be an object.

{
message:"Phone not valid",
excludeEmptyString:true
}

Example

  const PHONE_NO_REGEX = /^[0-9\- ]{8,14}$/
        const signUpSchema = yup.object().shape({
        ...
        phoneNumber: yup.string()
            .matches(PHONE_NO_REGEX, phoneNumber:yup.string().matches(PHONE_NO_REGEX,{message:"not valid phone no",excludeEmptyString:true}))
          
        ...
        });
like image 151
Dhanuka Perera Avatar answered Oct 17 '22 00:10

Dhanuka Perera