Write a java program to Validating Input Password.

Write a code get a password as string input and validate using the rules specified below. Apply following validations:

  1. Minimum length should be 8 characters.
  2. Must contain any one of these three special characters @ or _ or #.
  3. May contain numbers or alphabets.
  4. Should not start with a special character or number.
  5. Should not end with a special character.

Input and Output Format

  • Input consists of a string.
  • Output is a string Valid or Invalid.

Refer sample output for formatting specifications

Sample Input 1:

ashok_23

Sample Output 1:

Valid

Sample Input 2:

1980_200

Sample Output 2:

Invalid

Validating Input Password Program in Java

Following are the steps to validate passwords in Java:

  • First, input the password from the user.
  • Now, check for the following conditions:
    • First, the length should be greater than 8.
    • Next, check if the input string contains a special character.
  • Now, return the character value from the starting position. Then, check for the below condition
    • If the first character is alphabet or not. if it is, then extract the character from the last position of the input string.
    • Then, check if that character is an alphabet or digits.
  • If it is a digit then check if the input string contains digits and alphabet using the regular expression. Then, print valid.
  • Otherwise, if all the above conditions are not true then print Invalid.
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String a = sc.next();
    int d = 0;
    if (a.length() >= 8) {
      if (a.contains("#") || a.contains("@") || a.contains("_")) {
        char c = a.charAt(0);
        //System.out.println(c);
        if (Character.isAlphabetic(c)) {
          char dd = a.charAt(a.length() - 1);
          
          if ((Character.isAlphabetic(dd)) || (Character.isDigit(dd)))

          {
            if (a.matches(".*[0-9]{1,}.*")
                || a.matches(".*[a-zA-Z]{1,}.*")) {

              System.out.println("Valid");

            } else{
              System.out.println("Not Valid");
            }
          } else{
            System.out.println("Not Valid");
          }
        }
         else{
            System.out.println("Not Valid");
          }
      } else{
        System.out.println("Not Valid");
      }

    } else{
      System.out.println("Not Valid");
    }
  }
}

Output

Write a java program to validate the Id

Write a program to get two string inputs and validate the ID as per the specified format. The return type of the output is a string Valid Id or Invalid Id.

Input and Output Format

  • Input consists of two strings.
  • The first string is ID and the second string is located. ID is in the format CBJ-LLL-XXXX where LLL is the first three letters of a given location and XXXX is a four-digit number.
  • The output is a string Valid id or Invalid id.

Refer sample output for formatting specifications

Sample Input 1:

CBJ-hyd-1234

hyderabad

Sample Output 1:

Valid id

Sample Input 2:

CBJ-hyd-123

hyderabad

Sample Output 2:

Invalid id

Program for Id validation in java

Following are the steps to validate ID against a given format:

  • Input Id and location from the user.
  • Pass both of them to formattingString() method.
  • Inside the method, get the substring from the location and store it in the s3 variable.
  • use a StringTokenizer class to break a string into tokens by passing the ‘-‘ as a delimiter. get each token and store it in variables s4,s5, and s6.
  • Now, check for each token whether the following condition is matched:
    • First, check if the first token equals “CBJ” also if the value in s5 equals that in s3.
    • Then, check if the token in the s6 variable contains 4 digit number.
  • If all the conditions are true then return Valid else Invalid. 
import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String s1 = sc.nextLine();
    String s2 = sc.nextLine();
    boolean b = formattingString(s1, s2);
    if (b == true)
      System.out.println("Valid");
    else
      System.out.println("Invalid");
  }

  public static boolean formattingString(String s1, String s2) {
    String s3 = s2.substring(0, 3);
    boolean b = false;
    StringTokenizer t = new StringTokenizer(s1, "-");
    String s4 = t.nextToken();
    String s5 = t.nextToken();
    String s6 = t.nextToken();
    if (s4.equals("CBJ") && s5.equals(s3) && s6.matches("[0-9]{4}"))
      b = true;
    else {
      b = false;
    }
    return b;
  }
}

Output

Thus, in this way, we can validate passwords and ID in Java.