我试着用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才能通过?
发布于 2018-11-08 14:49:16
嗨,现在我正在解决和你我一样的问题,找到了可能的解决方案。
使用匹配Regex的字符串验证电话号码
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')您可以搜索不同的Regex表达式并验证它。我使用了本文中的Regex ( https://www.sitepoint.com/community/t/phone-number-regular-expression-validation/2204 )
发布于 2019-04-26 10:53:32
>.更新.<
http://yup-phone.js.org/
我已经创建了一个使用对讲机的google-libphonenumber模块,它提供了精确的验证检查,并且可以直接从github安装
npm install --save yup yup-phone。
检查用法
const Yup = require('yup');
require('yup-phone');
// validate any phone number (defaults to India for country)
const phoneSchema = Yup.string().phone().required();
phoneSchema.isValid('9876543210'); // → true来自简单反应气浮器,
电话号码验证的正则表达式是
/^(\+?\d{0,4})?\s?-?\s?(\(?\d{3}\)?)\s?-?\s?(\(?\d{3}\)?)\s?-?\s?(\(?\d{4}\)?)?$/示例
// index.js
const yup = require('yup');
const { rePhoneNumber } = require('./yup-phone')
const schema = yup.string().phone()
const phone = '+911234567890';
console.log('Is Valid? ', rePhoneNumber.test(phone)); // Is Valid? true
schema.validateSync(phone);// yup-phone.js
const yup = require('yup');
const rePhoneNumber = /^(\+?\d{0,4})?\s?-?\s?(\(?\d{3}\)?)\s?-?\s?(\(?\d{3}\)?)\s?-?\s?(\(?\d{4}\)?)?$/;
module.exports.rePhoneNumber = rePhoneNumber
yup.addMethod(yup.string, "phone", function() {
return this.test("phone", "Phone number is not valid", value =>
rePhoneNumber.test(value)
);
});发布于 2019-01-04 06:29:15
试试这个,也许对你有帮助。
移动电话: Yup.string().matches(/^6-9\d{9}$/,{消息:“请输入有效号码”,excludeEmptyString: false})
https://stackoverflow.com/questions/52483260
复制相似问题