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)
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) { // Define the pattern for a valid mobile number Pattern pattern = Pattern.compile( "^\\d{10}$" ); // Test the mobile number against the pattern return pattern.matcher(mobileNumber).matches(); } public static void main(String[] args) { // Testing the function 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)) # Testing the function 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); } // Testing the function 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`); } } |
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): # convert the mobile number to a numpy array of integers s_array = np.array( list (s), dtype = int ) # check if the array meets the validation criteria 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 () ) # Driver Code s = "347873923408" if (isValid(s)): print ( "Valid Number" ) else : print ( "Invalid Number" ) #This code is contributed by Jyothi pinjala. |
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.