Program to check valid mobile number
Last Updated :
19 Apr, 2023
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++
#include <iostream>
#include <regex>
using namespace std;
bool isValid(string s)
{
const regex pattern( "(0|91)?[6-9][0-9]{9}" );
if (regex_match(s, pattern))
{
return true ;
}
else
{
return false ;
}
}
int main()
{
string s = "347873923408" ;
if (isValid(s))
{
cout << "Valid" ;
}
else
{
cout<< "Invalid" ;
}
return 0;
}
|
Java
import java.util.regex.*;
import java.util.Scanner;
class GFG{
public static boolean isValid(String s)
{
Pattern p = Pattern.compile( "(0|91)?[6-9][0-9]{9}" );
Matcher m = p.matcher(s);
return (m.find() && m.group().equals(s));
}
public static void main(String[] args)
{
String s = "347873923408" ;
if (isValid(s))
System.out.println( "Valid Number" );
else
System.out.println( "Invalid Number" );
}
}
|
Python
import re
def isValid(s):
Pattern = re. compile ( "(0|91)?[6-9][0-9]{9}" )
return Pattern.match(s)
s = "347873923408"
if (isValid(s)):
print ( "Valid Number" )
else :
print ( "Invalid Number" )
|
C#
using System;
using System.Text.RegularExpressions;
class GFG {
static void Main( string [] args)
{
string str = "347873923408" ;
if (isValid(str)) {
Console.WriteLine( "Valid Number" );
}
else {
Console.WriteLine( "Invalid Number" );
}
}
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 );
}
}
|
Javascript
function isValid_Mobile_Number(mobile_number) {
let regex = new RegExp(/(0|91)?[6-9][0-9]{9}/);
if (mobile_number == null ) {
return "false" ;
}
if (regex.test(mobile_number) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "9136812895" ;
console.log(isValid_Mobile_Number(str1));
let str2 = "7873923408" ;
console.log(isValid_Mobile_Number(str2));
let str3 = "5678729234" ;
console.log(isValid_Mobile_Number(str3));
let str4 = "09793295673" ;
console.log(isValid_Mobile_Number(str4));
|
Output:
Invalid Number
Time Complexity: O(N) where N is the length of the given string.
Auxiliary Space: O(1)
Approach:
Import the re module to work with regular expressions.
Define a function validate_mobile_number that takes a string input mobile_number.
Compile a regular expression pattern r’^\d{10}$’ that matches a string with exactly 10 digits from start to end using re.compile().
Use bool(pattern.match(mobile_number)) to check if the input string matches the pattern or not. If it matches, it will return True, and False otherwise.
Test the function by providing a list of sample inputs to it.
Loop through each sample input, pass it to the validate_mobile_number function, and print a message indicating whether the input is a valid mobile number or not.
Java
import java.util.regex.Pattern;
public class Main {
public static boolean validateMobileNumber(String mobileNumber) {
Pattern pattern = Pattern.compile( "^\\d{10}$" );
return pattern.matcher(mobileNumber).matches();
}
public static void main(String[] args) {
String[] mobileNumbers = { "9876543210" , "1234567890" , "1234" , "12345678901" , "123456789a" };
for (String mobileNumber : mobileNumbers) {
if (validateMobileNumber(mobileNumber)) {
System.out.println(mobileNumber + " is a valid mobile number" );
} else {
System.out.println(mobileNumber + " is an invalid mobile number" );
}
}
}
}
|
Python3
import re
def validate_mobile_number(mobile_number):
pattern = re. compile (r '^\d{10}$' )
return bool (pattern.match(mobile_number))
mobile_numbers = [ '9876543210' , '1234567890' , '1234' , '12345678901' , '123456789a' ]
for mobile_number in mobile_numbers:
if validate_mobile_number(mobile_number):
print (f "{mobile_number} is a valid mobile number" )
else :
print (f "{mobile_number} is an invalid mobile number" )
|
Javascript
function validateMobileNumber(mobileNumber) {
const pattern = /^\d{10}$/;
return pattern.test(mobileNumber);
}
const mobileNumbers = [ '9876543210' , '1234567890' , '1234' , '12345678901' , '123456789a' ];
for (const mobileNumber of mobileNumbers) {
if (validateMobileNumber(mobileNumber)) {
console.log(`${mobileNumber} is a valid mobile number`);
} else {
console.log(`${mobileNumber} is an invalid mobile number`);
}
}
|
Output
9876543210 is a valid mobile number
1234567890 is a valid mobile number
1234 is an invalid mobile number
12345678901 is an invalid mobile number
123456789a is an invalid mobile number
The time complexity of O(1), as the regular expression pattern matching is a constant time operation.
The auxiliary space of this approach is also O(1),
Approach#3: Using numpy:
Algorithm:
- Define a function called isValid() that takes a string s as an argument.
- Define a regular expression pattern that matches mobile numbers that begin with either 0 or 91, followed by a digit between 6 and 9, and then 9 more digits.
- Use the re module’s compile() function to compile the pattern into a regular expression object.
- Use the regular expression object’s match() function to check if the string s matches the pattern.
- If s matches the pattern, return True. Otherwise, return False.
- In the driver code, define a string variable s that contains the mobile number to be checked.
- Call the isValid() function with s as the argument.
- If the function returns True, print “Valid Number”. Otherwise, print “Invalid Number”.
Python3
import numpy as np
import re
def isValid(s):
s_array = np.array( list (s), dtype = int )
return (
((s_array[ 0 ] = = 0 ) or (s_array[: 2 ] = = [ 9 , 1 ])). all () and
(np.isin(s_array[ 2 ], [ 6 , 7 , 8 , 9 ])) and
( len (s_array) = = 11 ) and
(np.isin(s_array[ 3 :], np.arange( 10 ))). all ()
)
s = "347873923408"
if (isValid(s)):
print ( "Valid Number" )
else :
print ( "Invalid Number" )
|
Output:
Invalid Number
Time complexity:
The time complexity of this code is primarily determined by the regular expression pattern matching. The match() method has a worst-case time complexity of O(n), where n is the length of the input string. However, the regular expression pattern used in this code is quite simple and has a constant time complexity that is independent of the input string length. Therefore, the overall time complexity of this code is O(1), or constant time.
Auxiliary Space:
The space complexity of this code is determined by the size of the regular expression pattern, which is constant and independent of the input string length. Therefore, the space complexity of this code is O(1), or constant space.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...