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

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

检查有效手机号码的程序

手机号码验证标准:

  • 第一个数字应包含 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正则表达式

C++
// C++ program to check if given mobile number
// is valid.
#include 
#include 
using namespace std;
  
bool isValid(string s)
{
    // The given argument to pattern()
    // is regular expression. With the help of
    // regular expression we can validate mobile
    // number.
    // 1) Begins with 0 or 91
    // 2) Then contains 7 or 8 or 9.
    // 3) Then contains 9 digits
  const regex pattern("(0|91)?[7-9][0-9]{9}");
  
  // regex_match() is used to
  // to find match between given number
  // and regular expression
  if(regex_match(s, pattern))
  {
    return true;
  }
  else
  {
    return false;
  }
}
  
// Driver Code
int main()
{
  string s = "347873923408";
  if(isValid(s))
  {
      cout << "Valid";
  }
  else
  {
      cout<<"Invalid";
  }
  return 0;
}
  
// This code is contributed by yuvraj_chandra


Java
// Java program to check if given mobile number
// is valid.
import java.util.regex.*;
import java.util.Scanner;
  
class GFG{
      
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. 
    // 1) Begins with 0 or 91
    // 2) Then contains 7 or 8 or 9.
    // 3) Then contains 9 digits
    Pattern p = Pattern.compile("(0|91)?[7-9][0-9]{9}");
  
    // Pattern class contains matcher() method
    // to find matching between given number 
    // and regular expression
    Matcher m = p.matcher(s);
    return (m.find() && m.group().equals(s));
}
  
// Driver code
public static void main(String[] args)
{
    String s = "347873923408";
      
    if (isValid(s)) 
        System.out.println("Valid Number");     
    else
        System.out.println("Invalid Number");     
}
}


Python
# Python program to check if 
# given mobile number is valid
import re
  
def isValid(s):
      
    # 1) Begins with 0 or 91
    # 2) Then contains 7 or 8 or 9.
    # 3) Then contains 9 digits
    Pattern = re.compile("(0|91)?[7-9][0-9]{9}")
    return Pattern.match(s)
  
# Driver Code
s = "347873923408"
if (isValid(s)): 
    print ("Valid Number")     
else :
    print ("Invalid Number") 
  
  
# This code is contributed by rishabh_jain


输出:

Invalid Number