Validation using Yup to check string or number length

reectrix picture reectrix · Apr 17, 2018 · Viewed 49.7k times · Source

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).

Answer

Tamlyn picture Tamlyn · Apr 20, 2018

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