简体   繁体   English

如何实现电话号码验证?

[英]how to implement validation for phone number?

I've created a form and need to validate field by it's content (empty, .empty) and phone number field so phone number match regex (ukrainian phone number).我创建了一个表单,需要通过它的内容(空,.empty)和电话号码字段来验证字段,以便电话号码匹配正则表达式(乌克兰电话号码)。 this option does not work (此选项不起作用(

  const regex = /^\+[0-9]{3}\s\((\d+)\)-\d{3}-\d{2}-\d{2}/g
  const validate = (username, phone, comment) => {
    if ((username.length === 0 || phone.length === 0, comment.length === 0)) {
   
      setErrorEmpty('the field is empty !')
    
    } else if (!phone.match.regex) {
     
      setPhoneErr('the format is wrong !')
     
    } else {
      setValidated(true)
    }
  }

ukrainian number consists of 10 digits: ex: 0954343333 or (38)0954343333乌克兰号码由 10 位数字组成:例如: 0954343333(38)0954343333

The regex is correct but you need to use the regex correctly.正则表达式是正确的,但您需要正确使用正则表达式。

Here are the docs这是文档

Try this, which asserts if the phone matches the regex:试试这个,它断言手机是否与正则表达式匹配:

 } else if (!regex.test(phone)) {

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

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