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 special character or number.
  5. Should not end with 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 in Java.

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");
    }
  }
}

Write a java program to validate 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 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

Id validation in java

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;
  }
}

Add a Comment

Your email address will not be published. Required fields are marked *

2 × 1 =