Skip to content
Related Articles
Open in App
Not now

Related Articles

Program to check valid mobile number

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 26 Dec, 2022
Improve Article
Save Article

Mobile Number validation criteria:

  • The first digit should contain numbers between 6 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

C++




// C++ program to check if given mobile number
// is valid.
#include <iostream>
#include <regex>
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 6,7 or 8 or 9.
    // 3) Then contains 9 digits
  const regex pattern("(0|91)?[6-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 6,7 or 8 or 9.
    // 3) Then contains 9 digits
    Pattern p = Pattern.compile("(0|91)?[6-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 6,7 or 8 or 9.
    # 3) Then contains 9 digits
    Pattern = re.compile("(0|91)?[6-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


C#




// C# program to validate the Mobile
// Number using Regular Expressions
using System;
using System.Text.RegularExpressions;
 
class GFG {
    // Main Method
    static void Main(string[] args)
    {
        // Input strings to Match
        // valid mobile number
        string str = "347873923408";
        if (isValid(str)) {
            Console.WriteLine("Valid Number");
        }
        else {
            Console.WriteLine("Invalid Number");
        }
    }
 
    // method containing the regex
    public static bool isValid(string str)
    {
        string strRegex = @"^(0|91)?[6-9][0-9]{9}$";
        Regex re = new Regex(strRegex);
        if (re.IsMatch(str))
            return (true);
        else
            return (false);
    }
}
//This code is contributed by Rahul Chauhan


Javascript




// Javascript program to check
// valid Mobile Number
 
// Function to validate the
// Mobile Number 
function isValid_Mobile_Number(mobile_number) {
    // Regex to check valid
    // mobile_number 
    let regex = new RegExp(/(0|91)?[6-9][0-9]{9}/);
 
    // if mobile_number
    // is empty return false
    if (mobile_number == null) {
        return "false";
    }
 
    // Return true if the mobile_number
    // matched the ReGex
    if (regex.test(mobile_number) == true) {
        return "true";
    }
    else {
        return "false";
    }
}
 
// Driver Code
// Test Case 1:
let str1 = "9136812895";
console.log(isValid_Mobile_Number(str1));
 
// Test Case 2:
let str2 = "7873923408";
console.log(isValid_Mobile_Number(str2));
 
// Test Case 3:
let str3 = "5678729234";
console.log(isValid_Mobile_Number(str3));
 
// Test Case 4:
let str4 = "09793295673";
console.log(isValid_Mobile_Number(str4));
 
 
// This code is contributed by Rahul Chauhan


Output:

Invalid Number

Time Complexity: O(N) where N is the length of the given string. 
Auxiliary Space: O(1)  


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!