Warm tip: This article is reproduced from stackoverflow.com, please click
javascript reactjs formik yup

Validation using Yup to check string or number length

发布于 2020-03-27 10:26:42

Is there a yup function that validates a specific length?

I tried .min(5) and .max(5), but I want something that ensures the number is exactly 5 characters (ie, zip code).

Questioner
reectrix
Viewed
164
Tamlyn 2018-04-20 18:08

I don't think there's anything built in but it's easy to implement with test:

yup.string()
  .test('len', 'Must be exactly 5 characters', val => val.length === 5)

https://runkit.com/tamlyn/5ad9b99a4ba1230012d7ac21