I have a project with React and next.js. I use formik for handling my forms and Yup for validations I have an input and I want perform some validations on it.
this is my schema
Yup.string().required("Requiredddd").matches(,/\d/, 'Wrongggg'). ,matches(/^[-ۿ\s]+$/, 'Only persian chars')
But in this case condition number 2 always Considered wrong.i think (!/\d/) is wrong but I haven't any idea how can use matches function Negatively
According to yup you can use .matches
using this shape: string.matches(regex: Regex, message?: string | function): Schema
. If you want to validate that the string doesn't contain numbers, instead of using (!/\d/)
you should use /\D+$/
.
Yup.string()
.required("Requiredddd")
.matches(/\D+$/, 'Wrongggg')
.matches(/^[\u0600-\u06FF\s]+$/, 'Only persian chars')
See working example
You can do it with the test()
function from Yup like the following:
.test('name Your Test here', 'your validation message', (value) => !yourRegex.test(value))
I believe Yup should provide an opposite function for .match()
since you don't always want to match, sometimes you want to test your value against the regex.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.