email: Yup.string() .email('E-mail is not valid!') .required('E-mail is required!'),
address: yup.string().required('Address is required'), city: yup.string().required('City is required'), region: yup.string().required('Region is required'), country: yup.string().required('Country is required').default('Afghanistan'), postalCode: yup .string() .when('country', { is: 'United States', then: yup.string().matches(/^[0-9]{5}(?:-[0-9]{4})?$/, 'Invalid postal code'), }) .when('country', { is: 'Canada', then: yup.string().matches(/^[A-Za-z]\d[A-Za-z][ -]?\d[A-Za-z]\d$/, 'Invalid postal code'), }) .required(),
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')
Plus spécifiquement pour le format de différents pays:
phone: yup .string() .when('country', { is: country => ["United States", "Canada"].includes(country), then: yup.string().matches(/^[2-9]\d{2}[2-9]\d{2}\d{4}$/, 'Invalid phone nunber') }) .required(),
password: Yup.string() .min(6, 'Password has to be longer than 6 characters!') .required('Password is required!'), passwordConfirmation: Yup.string() .oneOf([values.password], 'Passwords are not the same!') .required('Password confirmation is required!'),
consent: Yup.bool() .test( 'consent', 'You have to agree with our Terms and Conditions!', value => value === true ) .required( 'You have to agree with our Terms and Conditions!' ),