PasswordValidation.sh
· 2.3 KiB · Bash
Raw
#!/bin/bash
#Validation Function:
#- Checks if the password is not empty.
#- Ensures the password length is between 5 and 16 characters.
#- Ensures the password is not the same as the username.
#- Checks if the password contains no spaces.
#- Ensures the password contains at least one uppercase letter.
#- Ensures the password contains at least one number.
#- Ensures the password contains at least one special character.
#Input and Validation:
#- Prompts the user to enter a username and password.
#- Calls the validation function to check the entered password.
#- Displays appropriate messages based on the validation result.
# Function to validate the password
function validate_password() {
local password="$1"
local username="$2"
# Check if password is not empty
if [[ -z "$password" ]]; then
echo "Password cannot be empty."
return 1
fi
# Check if password length is between 5 and 16 characters
if [[ ${#password} -lt 5 ]] || [[ ${#password} -gt 16 ]]; then
echo "Password must be between 5 and 16 characters long."
return 1
fi
# Check if password is the same as the username
if [[ "$password" == "$username" ]]; then
echo "Password cannot be the same as the username."
return 1
fi
# Check if password contains spaces
if [[ "$password" =~ \ ]]; then
echo "Password cannot contain spaces."
return 1
fi
# Check if password contains at least one uppercase letter
if ! [[ "$password" =~ [A-Z] ]]; then
echo "Password must contain at least one uppercase letter."
return 1
fi
# Check if password contains at least one number
if ! [[ "$password" =~ [0-9] ]]; then
echo "Password must contain at least one number."
return 1
fi
# Check if password contains at least one special character
if ! [[ "$password" =~ [^a-zA-Z0-9] ]]; then
echo "Password must contain at least one special character."
return 1
fi
return 0
}
# Get username and password input from the user
read -p "Enter username: " username
read -s -p "Enter password: " password
echo
# Validate the password
if validate_password "$password" "$username"; then
echo "Password is valid."
else
echo "Password is invalid. Please try again."
fi
| 1 | #!/bin/bash |
| 2 | |
| 3 | #Validation Function: |
| 4 | #- Checks if the password is not empty. |
| 5 | #- Ensures the password length is between 5 and 16 characters. |
| 6 | #- Ensures the password is not the same as the username. |
| 7 | #- Checks if the password contains no spaces. |
| 8 | #- Ensures the password contains at least one uppercase letter. |
| 9 | #- Ensures the password contains at least one number. |
| 10 | #- Ensures the password contains at least one special character. |
| 11 | |
| 12 | #Input and Validation: |
| 13 | #- Prompts the user to enter a username and password. |
| 14 | #- Calls the validation function to check the entered password. |
| 15 | #- Displays appropriate messages based on the validation result. |
| 16 | |
| 17 | # Function to validate the password |
| 18 | function validate_password() { |
| 19 | local password="$1" |
| 20 | local username="$2" |
| 21 | |
| 22 | # Check if password is not empty |
| 23 | if [[ -z "$password" ]]; then |
| 24 | echo "Password cannot be empty." |
| 25 | return 1 |
| 26 | fi |
| 27 | |
| 28 | # Check if password length is between 5 and 16 characters |
| 29 | if [[ ${#password} -lt 5 ]] || [[ ${#password} -gt 16 ]]; then |
| 30 | echo "Password must be between 5 and 16 characters long." |
| 31 | return 1 |
| 32 | fi |
| 33 | |
| 34 | # Check if password is the same as the username |
| 35 | if [[ "$password" == "$username" ]]; then |
| 36 | echo "Password cannot be the same as the username." |
| 37 | return 1 |
| 38 | fi |
| 39 | |
| 40 | # Check if password contains spaces |
| 41 | if [[ "$password" =~ \ ]]; then |
| 42 | echo "Password cannot contain spaces." |
| 43 | return 1 |
| 44 | fi |
| 45 | |
| 46 | # Check if password contains at least one uppercase letter |
| 47 | if ! [[ "$password" =~ [A-Z] ]]; then |
| 48 | echo "Password must contain at least one uppercase letter." |
| 49 | return 1 |
| 50 | fi |
| 51 | |
| 52 | # Check if password contains at least one number |
| 53 | if ! [[ "$password" =~ [0-9] ]]; then |
| 54 | echo "Password must contain at least one number." |
| 55 | return 1 |
| 56 | fi |
| 57 | |
| 58 | # Check if password contains at least one special character |
| 59 | if ! [[ "$password" =~ [^a-zA-Z0-9] ]]; then |
| 60 | echo "Password must contain at least one special character." |
| 61 | return 1 |
| 62 | fi |
| 63 | |
| 64 | return 0 |
| 65 | } |
| 66 | |
| 67 | # Get username and password input from the user |
| 68 | read -p "Enter username: " username |
| 69 | read -s -p "Enter password: " password |
| 70 | echo |
| 71 | |
| 72 | # Validate the password |
| 73 | if validate_password "$password" "$username"; then |
| 74 | echo "Password is valid." |
| 75 | else |
| 76 | echo "Password is invalid. Please try again." |
| 77 | fi |
| 78 |