gpt4 book ai didi

javascript - 使用 Yup 验证以检查字符串或数字长度

转载 作者:可可西里 更新时间:2023-11-01 02:18:22 26 4
gpt4 key购买 nike

是否有验证特定长度的 yup 函数?

我尝试了 .min(5).max(5),但我想要确保数字恰好为 5 个字符(即邮政编码)的东西。

最佳答案

此检查可带来最佳验证体验:

Yup.string()
.required()
.matches(/^[0-9]+$/, "Must be only digits")
.min(5, 'Must be exactly 5 digits')
.max(5, 'Must be exactly 5 digits')

输出:

12f1    // Must be only digits
123 // Must be exactly 5 digits
123456 // Must be exactly 5 digits
01234 // valid
11106 // valid

演示: https://codesandbox.io/s/yup-y6uph

关于javascript - 使用 Yup 验证以检查字符串或数字长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49886881/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com