Related Articles

Related Articles

Program to check valid mobile number
  • Last Updated : 17 Sep, 2020

Mobile Number validation criteria:

  • The first digit should contain number between 7 to 9.
  • The rest 9 digit can contain any number between 0 to 9.
  • The mobile number can have 11 digits also by including 0 at the starting.
  • The mobile number can be of 12 digits also by including 91 at the starting

The number which satisfies the above criteria, is a valid mobile Number. 
Examples:

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

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

Prerequisites : Java Regular Expressions

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");     
}
}

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 

chevron_right


Output:

Invalid Number

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :