Write a java program check Number Validation.

Write a program to read a string of 10-digit numbers, and check whether the string contains a 10-digit number in the format XXX-XXX-XXXX where ‘X’ is a digit(Number Validation).

Input and Output Format

  • Input consists of a string.
  • The output is a string specifying whether the given string is valid or not.

Refer sample output for formatting specifications

Sample Input 1:

123-456-7895

Sample Output 1:

Valid number format

Sample Input 2:

-123-12344322

Sample Output 2:

Invalid number format

Number Validation in Java

Following are the steps we will use to validate the number:

  • Input a number.
  • Use the following regex to validate the number:
    • [0-9]{3} – First, there should be a number from 0-9 which should be 3 digits.
    • [-]{1} – It represents that the 3 digits should be followed by -(hyphen).
    • [0-9]{3} – Next, three digits from 0-9.
    • [-] {1}- Again, there should be one hyphen.
    • [0-9]{4} – It represents the number from 0-9 that should be of 4 digits.
  • At last, check if the input follows the above pattern then print “Valid number format” otherwise “Invalid number format”.
import java.util.Scanner;


public class Main2 {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
     String s1 = sc.nextLine();
    
    if(s1.matches("[0-9]{3}[-]{1}[0-9]{3}[-]{1}[0-9]{4}")){
      System.out.println("Valid number format");
    }else{
      System.out.println("Invalid number format");
    }
  }

}

Output

Validating Mobile Number in Java

Write a program to read a string of 10-digit numbers, and check whether the string contains a 10-digit number in the format XXXXXXXXXX where ‘X’ is a digit(Number Validation). (no special character)

Input and Output Format

  • Input consists of a string.
  • The output is a string specifying whether the given string is valid or not.

Refer sample output for formatting specifications

Sample Input 1:

7777777777

Sample Output 1:

Valid number format

Sample Input 2:

4777777777

Sample Output 2:

Invalid number format

Program to validate Mobile Numbers in Java

Following are the steps we will use to validate the number:

  • Input a number.
  • Use the following regex to validate the number:
    • [7-9] – The first digit should be either 7,8 or 9.
    • [0-9]{9} – The next pattern should be the combination of digits of length 9.
  • At last, check if the input follows the above pattern then print “Valid number format” otherwise “Invalid number format”.
package com.company;

import java.util.Scanner;

public class NumberValidation {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s1 = sc.nextLine();
        if(s1.matches("[7-9][0-9]{9}")){
            System.out.println("Valid number format");
        }
        else{
            System.out.println("Invalid number format");
        }
    }
}

Output

Indian Phone Number Validation in Java

Write a program to read a string of 10 digit number, check whether the string contains a 10 digit number in the format YYXXXXXXXXX where ‘X’ is a digit(Number Validation) and Y is either 0|91. (no special character)

Input and Output Format

  • Input consists of a string.
  • The output is a string specifying the given string is valid or not.

Refer sample output for formatting specifications

Sample Input 1:

917777777777

Sample Output 1:

Valid number format

Sample Input 2:

4777777777

Sample Output 2:

Invalid number format

Program to validate Indian Mobile Number in Java

Following are the steps we will use to validate the number:

  • Input a number.
  • Use the following regex to validate the number:
    • [0|91]?: It should start with 0 or 91.
    • [7-9] – The first digit should be either 7,8 or 9.
    • [0-9]{9} – The next pattern should be the combination of digits of length 9.
  • At last, check if the input follows the above pattern then print “Valid number format” otherwise “Invalid number format”.
package com.company;

import java.util.Scanner;

public class NumberValidation {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s1 = sc.nextLine();
        if(s1.matches("(0|91)?[7-9][0-9]{9}")){
            System.out.println("Valid number format");
        }
        else{
            System.out.println("Invalid number format");
        }
    }
}

Output

Thus, in this way, we learn different ways to validate a number again a particular pattern by using regex.