我正在尝试使用 Yup 验证电话号码:
phone: Yup.number()
.typeError("That doesn't look like a phone number")
.positive("A phone number can't start with a minus")
.integer("A phone number can't include a decimal point")
.min(8)
.required('A phone number is required'),
.min(8)
验证数字是否为 8 或更多。因此只需输入 8
即可通过。如何让 1000 0000
需要 8 个字符才能通过?
最佳答案
您好,我现在正在解决与您相同的问题,并且我找到了可能的解决方案。
使用与正则表达式匹配的字符串验证电话号码
const phoneRegExp = /^((\\+[1-9]{1,4}[ \\-]*)|(\\([0-9]{2,3}\\)[ \\-]*)|([0-9]{2,4})[ \\-]*)*?[0-9]{3,4}?[ \\-]*[0-9]{3,4}?$/
phoneNumber: Yup.string().matches(phoneRegExp, 'Phone number is not valid')
您可以搜索不同的正则表达式并验证它。 我使用了这篇文章中的正则表达式 https://www.sitepoint.com/community/t/phone-number-regular-expression-validation/2204
关于javascript - 使用 Yup 验证电话号码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52483260/