简体   繁体   English

用 Yup 验证电话号码?

[英]Validate phone number with Yup?

I'm trying to validate a phone number with Yup:我正在尝试使用 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) validates that the number is 8 or more. .min(8)验证数字是否为 8 或更多。 So simply entering 8 will pass.所以只要输入8就可以通过。 How can I make 8 characters required so 1000 0000 would pass?如何使 8 个字符成为必需的,以便1000 0000可以通过?

Hi right now I'am solving same problem as you and I found possible solution.嗨,现在我正在解决和你一样的问题,我找到了可能的解决方案。

Validate phone number with string that matches 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')

You can search for different Regex Expressions and validate it.您可以搜索不同的 Regex 表达式并对其进行验证。 I've used Regex from this article https://www.sitepoint.com/community/t/phone-number-regular-expression-validation/2204我在这篇文章中使用了正则表达式https://www.sitepoint.com/community/t/phone-number-regular-expression-validation/2204

>. >. Update .<更新.<

http://yup-phone.js.org/ http://yup-phone.js.org/

I've created a yup-phone module that uses google-libphonenumber which gives accurate validation checks and can be installed directly from github我创建了一个yup-phone模块,它使用google-libphonenumber提供准确的验证检查,并且可以直接从 github 安装

npm install --save yup yup-phone . npm install --save yup yup-phone

Check Usage检查使用情况

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


From Simple React Validator ,简单的反应验证器

The regex for phone number validation is电话号码验证的正则表达式是

/^(\+?\d{0,4})?\s?-?\s?(\(?\d{3}\)?)\s?-?\s?(\(?\d{3}\)?)\s?-?\s?(\(?\d{4}\)?)?$/

Example例子

// 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)
  );
});

Try this, it might be helpful for you.试试这个,它可能对你有帮助。

mobile: Yup.string().matches(/^[6-9]\\d{9}$/, {message: "Please enter valid number.", excludeEmptyString: false}) mobile: Yup.string().matches(/^[6-9]\\d{9}$/, {message: "请输入有效号码。", excludeEmptyString: false})

 const phoneRegExp = /^((\\\\+[1-9]{1,4}[ \\\\-]*)|(\\\\([0-9]{2,3}\\\\)[ \\\\-]*)|([0-9]{2,4})[ \\\\-]*)*?[0-9]{3,4}?[ \\\\-]*[0-9]{3,4}?$/ phone_number: Yup.string() .required("required") .matches(phoneRegExp, 'Phone number is not valid') .min(10, "to short") .max(10, "to long"),

This works best for me...you can set your own length...i just wanted 10 digits not less or more这对我来说最有效……你可以设置自己的长度……我只想要 10 位数字,不少于或更多

Just a little collaboration.只是一点点合作。 On my case I don't want to validate if the input is empty (when is not required).在我的情况下,我不想验证输入是否为空(不需要时)。 Thank you all for your examples!谢谢大家的例子!

yup.addMethod(yup.string, "phone", function(messageError = 'Phone number is not valid') {
    const phoneRegExp = /^((\\+[1-9]{1,4}[ \\-]*)|(\\([0-9]{2,3}\\)[ \\-]*)|([0-9]{2,4})[ \\-]*)*?[0-9]{3,4}?[ \\-]*[0-9]{3,4}?$/
    return this.test('phone', messageError, value => {
      if (value && value.length > 0) {
        return phoneRegExp.test(value)
      }
      return true
    })
})

I have a similar use case, and here's my solution:我有一个类似的用例,这是我的解决方案:

// Regex source: https://www.oreilly.com/library/view/regular-expressions-cookbook/9781449327453/ch04s02.html

const phoneRegex = RegExp(
  /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/
);

const schema = yup.object().shape({
  phone: yup.string().matches(phoneRegex, "Invalid phone").required("Phone is required")
});

Yup.string().trim().required().matches(/^\\d+$/, '格式不正确').length(10, '必须是 10 位数字')

I made a new package for this called yup-phone-lite .我为此制作了一个名为yup-phone-lite的新软件包。 I had used the original yup-phone package but it uses the massive google-libphonenumber so I replaced it with a smaller fork.我使用了原来的 yup-phone 包,但它使用了大量的 google-libphonenumber,所以我用一个较小的叉子代替了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM