代码之家  ›  专栏  ›  技术社区  ›  Evanss

验证电话号码?

  •  3
  • Evanss  · 技术社区  · 6 年前

    我正在用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 会过去吗?

    2 回复  |  直到 6 年前
        1
  •  60
  •   filippofilip    6 年前

    嗨,现在我正在解决和你一样的问题,我找到了可能的解决办法。

    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

        2
  •  25
  •   abhisekp    4 年前

    >. 更新。<

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

    yup-phone 使用的模块 google-libphonenumber 它提供了准确的验证检查,可以直接从github安装

    npm install --save yup yup-phone

    检查 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
    


    Simple React Validator ,

    /^(\+?\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)
      );
    });
    

        3
  •  8
  •   Sirisha    6 年前

    试试这个,可能对你有帮助。

    mobile:Yup.string().matches(/^[6-9]\d{9}$/,{message:“请输入有效数字”,excludeEmptyString:false})

        4
  •  2
  •   Vipul Singh    4 年前

    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"),

        5
  •  1
  •   adrxlm    4 年前

    只是一点合作。在我的例子中,我不想验证输入是否为空(不需要时)。谢谢大家的例子!

    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
        })
    })
    
        6
  •  1
  •   calvin    4 年前

    我有一个类似的用例,下面是我的解决方案:

    // 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")
    });
    
        7
  •  0
  •   A.Veryga    5 年前

    推荐文章