简体   繁体   English

Java中的电话号码验证

[英]Phone number validation in Java

I am using the following code to validate a phone number. 我使用以下代码验证电话号码。 The requirements are the phone number should be inbetween 10-25 characters length, should include hypen(-),period(.), parentheses (). 要求是电话号码应在10-25个字符之间,应包括hypen( - ),句点(。),括号()。

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class ValidatePhoneNumber {

public static void main(String[] argv) {

    String phoneNumber = "6058.8()6-05888,9994567";
    System.out.println(phoneNumber.length());
    //String sPhoneNumber = "605-88899991";
    //String sPhoneNumber = "605-888999A";
    String regex = "^[0-9.()-]{10,25}$";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(phoneNumber);

    if (matcher.matches()) {
        System.out.println("Phone Number Valid");
    } else {
        System.out.println("Phone Number must be in the form XXX-XXXXXXX");
    }
  }
 }

I checked the validations and its working fine, I want to add a whitespace character "\\s" as well so that in between the phone number there can be whitespace as well. 我检查了验证及其工作正常,我想添加一个空白字符“\\ s”,以便在电话号码之间也可以有空格。 But getting errors while adding "\\s" in regex. 但是在正则表达式中添加“\\ s”时会出错。

Please, look at standards like ITU E.164 or IETF RfC 3966. Don't assume that every country has the same conventions and number lengths. 请查看ITU E.164或IETF RfC 3966等标准。不要假设每个国家/地区都有相同的约定和数字长度。

Here's the relevant ABNF part out of RfC 3966 这是RfC 3966中相关的ABNF部分

global-number-digits = "+" *phonedigit DIGIT *phonedigit
phonedigit           = DIGIT / visual-separator 
visual-separator     = "-" / "." / "(" / ")"
DIGIT = "0" / "1" / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9"

We recently had a huge trouble with a device that didn't let the user enter the '+' as part of a phone number. 我们最近遇到了一个设备,它没有让用户输入“+”作为电话号码的一部分。

I would suggest not being so fussy with phone numbers, people like to use spaces and brackets in their own ways, and such strictness can simply irritate. 我建议不要对电话号码这么挑剔,人们喜欢用自己的方式使用空格和括号,这样的严格性可能会让人烦恼。 Why not restrict it to numbers only, but otherwise spaces/brackets etc are OK? 为什么不将它仅限于数字,否则空格/括号等都可以?

Also - sometimes international numbers use a '+' char. 此外 - 有时国际号码使用'+'字符。

You have this in a main() method, presumably for the purposes of posting here. 你有一个main()方法,大概是为了在这里发布。 However, if this code is run multiple times you should consider moving the regex compilation outside the called method. 但是,如果此代码多次运行,您应该考虑在调用方法之外移动正则表达式编译。 Compilation is potentially expensive. 编译可能很昂贵。

Note that java.util.regex.Pattern is thread safe, java.util.regex.Matcher is not so the standard idiom is something like: 请注意,java.util.regex.Pattern是线程安全的,java.util.regex.Matcher不是那么标准的习惯用法是这样的:

public class PhoneNumberValidator {

    private static Pattern VALID_PHONE_NUMBER = 
        Pattern.compile("^[0-9.()-]{10,25}$");

    public boolean isValidPhoneNumber(String s) {

         Matcher m = VALID_PHONE_NUMBER.matcher(s);

         return m.matches();
    }

}

我用这个正则表达式:“^(1 [\\ s - ]?)?(((\\ d {3}))|(\\ d {3}))[\\ s - ]?\\ d {3} [\\ s - ]?\\ d {4} $“这可能比我真正需要的还要多,特别是因为我在手机上工作所以人们很少使用parens或破折号,但这让我感到高兴。

Probably something like this would be better 可能这样的事情会更好

"\\(?[\\d]{3}\\)?\\-?[\\d]{w}\\-?[\\d]{4}"

Don't know how international that would be, but it should give you an idea. 不知道会有多国际化,但它应该给你一个想法。 The problem with yours is that it would capture a lot of weird number, hyphen and parenthesis combinations. 你的问题是它会捕获很多奇怪的数字,连字符和括号组合。

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

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