📌  相关文章
📜  用于检查有效手机号码的Java程序

📅  最后修改于: 2022-05-13 01:55:02.908000             🧑  作者: Mango

用于检查有效手机号码的Java程序

正则表达式或正则表达式(简称 Regex)是一种用于定义字符串模式的 API,可用于在Java搜索、操作和编辑字符串。电子邮件验证和密码是字符串的几个领域,其中 Regex 被广泛用于定义约束。正则表达式在Java.util.regex 包下提供。这包括 3 个类和 1 个接口。

在这里,我们将讨论两个示例,一个针对印度,另一个针对所有国家/地区进行标准验证。因此,案件如下:

  • 案例 1:本地电话号码是印度电话号码
  • 案例 2:全球电话号码

案例 1:本地电话号码是印度电话号码

手机号码验证标准如下:



  • 第一个数字应包含 7 到 9 之间的数字。
  • 其余 9 位数字可以包含 0 到 9 之间的任何数字。
  • 手机号码也可以有 11 位数字,也可以在开头包含 0。
  • 手机号码也可以是 12 位数字,也可以在开头包含 91。

插图:

Input  : Enter Mobile Number: 7873923408
Output : Valid Mobile Number
Input  : Enter Mobile Number: 5678729234
Output : Invalid Mobile Number

概念:

必须对正则表达式有很好的理解,因为它是先决条件。在Java的手机号码验证使用模式和Java的匹配器类来完成。模式类用于编译给定的模式/正则表达式,匹配器类用于将输入字符串与已编译的模式/正则表达式进行匹配。在这个方法中,提到了一个正则表达式,它提到了验证输入字符串的标准,在本文中是手机号码。印度的手机号码有 10 位数字,所以我们可以提到一个只包含 10 位数字的正则表达式。

例子:

Java
// Java Program to Check if Mobile Number is Valid or Not
 
// Importing all regex classes from java.util package to
// match character sequence against patterns
// Importing input output classes
import java.io.*;
import java.util.regex.*;
// Main class
class GFG {
 
    // Method 1
    // To check whether number is valid or not
    public static boolean isValid(String s)
    {
 
        // The given argument to compile() method
        // is regular expression. With the help of
        // regular expression we can validate mobile
        // number.
        // The number should be of 10 digits.
 
        // Creating a Pattern class object
        Pattern p = Pattern.compile("^\\d{10}$");
 
        // Pattern class contains matcher() method
        // to find matching between given number
        // and regular expression for which
        // object of Matcher class is created
        Matcher m = p.matcher(s);
 
        // Returning bollean value
        return (m.matches());
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
 
        // Considering two numbers as inputs
        // Custom input numbers
        String s = "7984286257";
        String s_1 = "5426391";
 
        // Checking over method 1 for string 1
        if (isValid(s))
 
            // Print statement
            System.out.println("Valid Number");
        else
 
            // Print statement
            System.out.println("Invalid Number");
 
        // Again, checking over method 1 for string 1
        if (isValid(s_1))
 
            // Print statement
            System.out.println("Valid Number");
        else
 
            // Print statement
            System.out.println("Invalid Number");
    }
}


Java
// Java program to Check if Mobile Number is Valid or Not
 
// Importing all regex classes for
// character sequences with pattern matching
import java.util.regex.*;
 
// Main class
class GFG {
 
    // Method 1
    //
    public static boolean isValid(String s)
    {
 
        // The given argument to compile() method
        // is regular expression. With the help of
        // regular expression we can validate mobile
        // number for which we create an object of
        // Pattern class
 
        Pattern p = Pattern.compile(
            "^(\\+\\d{1,3}( )?)?((\\(\\d{1,3}\\))|\\d{1,3})[- .]?\\d{3,4}[- .]?\\d{4}$");
 
        // Pattern class contains matcher() method
        // to find matching between given number
        // and regular expression by creating an object of
        // Matcher class
        Matcher m = p.matcher(s);
 
        // Returns boolean value
        return (m.matches());
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
 
        // Custom input strings
        String s = "+1 212 555-3458";
        String s_1 = "+4934 351 125-3456";
 
        // Checking if the strings are valid or not
        if (isValid(s))
 
            // Print statement
            System.out.println("Valid Number");
        else
 
            // Print statement
            System.out.println("Invalid Number");
 
        // Again, checking over another string(phone number)
        if (isValid(s_1))
 
            // Print statement
            System.out.println("Valid Number");
        else
 
            // Print statement
            System.out.println("Invalid Number");
    }
}



输出
Valid Number
Invalid Number

案例 2:全球电话号码



该方法再次借助正则表达式来匹配所有国家/地区格式的电话号码

手机号码验证标准:

  1. 国家/地区代码前缀以“+”开头,包含 1-3 位数字。
  2. 大多数国家/地区的最后 4 位数字都在连字符 (-) 之后。

满足验证的数字如下图所示:

插图:

Input  : Enter Mobile Number : +1 212 555-3458 (USA)
Output : Valid Mobile Number
Input  : Enter Mobile Number : +4934 351 125-3456
Output : Invalid Mobile Number

例子:

Java

// Java program to Check if Mobile Number is Valid or Not
 
// Importing all regex classes for
// character sequences with pattern matching
import java.util.regex.*;
 
// Main class
class GFG {
 
    // Method 1
    //
    public static boolean isValid(String s)
    {
 
        // The given argument to compile() method
        // is regular expression. With the help of
        // regular expression we can validate mobile
        // number for which we create an object of
        // Pattern class
 
        Pattern p = Pattern.compile(
            "^(\\+\\d{1,3}( )?)?((\\(\\d{1,3}\\))|\\d{1,3})[- .]?\\d{3,4}[- .]?\\d{4}$");
 
        // Pattern class contains matcher() method
        // to find matching between given number
        // and regular expression by creating an object of
        // Matcher class
        Matcher m = p.matcher(s);
 
        // Returns boolean value
        return (m.matches());
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
 
        // Custom input strings
        String s = "+1 212 555-3458";
        String s_1 = "+4934 351 125-3456";
 
        // Checking if the strings are valid or not
        if (isValid(s))
 
            // Print statement
            System.out.println("Valid Number");
        else
 
            // Print statement
            System.out.println("Invalid Number");
 
        // Again, checking over another string(phone number)
        if (isValid(s_1))
 
            // Print statement
            System.out.println("Valid Number");
        else
 
            // Print statement
            System.out.println("Invalid Number");
    }
}


输出
Valid Number
Invalid Number