yup code example
Example 1: yup only characters regex validation react
yup.string()
.required("Please enter the required field")
.matches(/^[aA-zZ\s]+$/, "Only alphabets are allowed for this field ")
Example 2: yup npm
npm i yup --save
Example 3: yup
import * as yup from 'yup';
let schema = yup.object().shape({
name: yup.string().required(),
age: yup.number().required().positive().integer(),
email: yup.string().email(),
website: yup.string().url(),
createdOn: yup.date().default(function () {
return new Date();
}),
});
schema
.isValid({
name: 'jimmy',
age: 24,
})
.then(function (valid) {
valid;
});
schema.cast({
name: 'jimmy',
age: '24',
createdOn: '2014-09-23T19:25:25Z',
});
Example 4: yup oneOf
let schema = yup.mixed().oneOf(['jimmy', 42]);
await schema.isValid(42);
await schema.isValid('jimmy');
await schema.isValid(new Date());