我正在尝试使用 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
将传递。我怎样才能使8个字符成为必需的1000 0000
会通过吗?
您好,现在我正在解决与您相同的问题,并且我找到了可能的解决方案。
使用与正则表达式匹配的字符串验证电话号码
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 https://www.sitepoint.com/community/t/phone-number-regular-expression-validation/2204
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)