Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
163
rated 0 times [  165] [ 2]  / answers: 1 / hits: 8478  / 5 Years ago, tue, november 19, 2019, 12:00:00

Yup.number().test('len', 'Must be exactly 5 characters', val => val && val.toString().length === 5)



It's not working correctly. If this field empty, gives a Must be exactly 5 characters, but should be show nothing.


More From » reactjs

 Answers
2

Try to add nullable() to your yup schema and a regular expression for the numbers, try something like this:



const regExp = /bd{5}b/;

Yup.string().matches(regExp, {message: 'Must be exactly 5 numbers', excludeEmptyString: true})

[#5550] Sunday, November 17, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
marisela

Total Points: 103
Total Questions: 105
Total Answers: 102

Location: Solomon Islands
Member since Fri, Oct 8, 2021
3 Years ago
;