I need to make a form validation using Yup to check input type email1
, email2
, email3
, email4
and email5
in my React App.
The rule of thumb is easy. Check those emails
is in valid form and cannot have similar emails
values.
I am super new to Yup. So what I did is:
const message = 'Duplicate emails not allowed';
const incorrect = 'Incorrect email format';
const Schema = Yup.object().shape({
email1: Yup.string()
.email(incorrect)
.oneOf([Yup.ref('email2'), null], message)
.oneOf([Yup.ref('email3'), null], message)
.oneOf([Yup.ref('email4'), null], message)
.oneOf([Yup.ref('email5'), null], message),
email2: Yup.string()
.email(incorrect)
.oneOf([Yup.ref('email1'), null], message)
.oneOf([Yup.ref('email3'), null], message)
.oneOf([Yup.ref('email4'), null], message)
.oneOf([Yup.ref('email5'), null], message),
email3: Yup.string()
.email(incorrect)
.oneOf([Yup.ref('email1'), null], message)
.oneOf([Yup.ref('email2'), null], message)
.oneOf([Yup.ref('email4'), null], message)
.oneOf([Yup.ref('email5'), null], message),
email4: Yup.string()
.email(incorrect)
.oneOf([Yup.ref('email1'), null], message)
.oneOf([Yup.ref('email2'), null], message)
.oneOf([Yup.ref('email3'), null], message)
.oneOf([Yup.ref('email5'), null], message),
email5: Yup.string()
.email(incorrect)
.oneOf([Yup.ref('email1'), null], message)
.oneOf([Yup.ref('email2'), null], message)
.oneOf([Yup.ref('email3'), null], message)
.oneOf([Yup.ref('email4'), null], message)
});
The issue with this method is, it will also detect an error of duplicate emails even the input text is empty. How do I fix this? Is there any better way that I could implement?
Thanks
Solution: const yup = require('yup') const { setLocale } = yup setLocale({ mixed: { notType: 'the ${path} is obligatory', required: 'the field ${path} is obligatory', oneOf: 'the field ${path} must have one of the following values: ${values}' } }) const myNameSchema = yup. object(). shape({ first_name: yup.
The Yup object schema provides two methods for validating: isValid and validate. isValid resolves true if the user submits a valid string, and false if an error exists in the string. validate returns an errors object if the input value is an invalid email.
When it comes to validation in JavaScript, no library comes to mind faster than Yup by JQuense. With Yup, the developer can define a schema (or structure) of the expected data specifying its data type and whether it is required or not.
you must use in this structure
const ValidationSchema=Yup.object().shape({email1: Yup.string().email(this.incorrect)
.notOneOf(
[
Yup.ref('email2'),
Yup.ref('email3'),
Yup.ref('email4'),
Yup.ref('email5')
],
this.message
),
email2: Yup.string()
.email(this.incorrect)
.notOneOf(
[
Yup.ref('email1'),
Yup.ref('email3'),
Yup.ref('email4'),
Yup.ref('email5')
],
this.message
),
email3: Yup.string()
.email(this.incorrect)
.notOneOf(
[
Yup.ref('email1'),
Yup.ref('email2'),
Yup.ref('email4'),
Yup.ref('email5')
],
this.message
),
email4: Yup.string()
.email(this.incorrect)
.notOneOf(
[
Yup.ref('email1'),
Yup.ref('email2'),
Yup.ref('email3'),
Yup.ref('email5')
],
this.message
),
email5: Yup.string()
.email(this.incorrect)
.notOneOf(
[
Yup.ref('email1'),
Yup.ref('email2'),
Yup.ref('email3'),
Yup.ref('email4')
],
this.message
)});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With