It there a way to validate a string that

>> Does not use characters like #$@ in the password.
>> It must contain 1 upper case letter, 1 lower case letter, 1 number & must end in a letter.
>> It must be 6-11 characters

using Regular Exp ?

Any pointer would be greatly apprecaited.

Cheers !

Recommended Answers

All 5 Replies

there are several different kinds of regex, is this for a passwd script?

some pretty standard ones are [A-Z] && [a-z] && [0-9], that should at least get you through the must require cap letters, lowercase letters and a number. The rest will depend on what you are running, such as awk, sed, grep, etc....
Let me know if this helps and what you are running this on and I'll be glad to help ya out.

I am kind of learning as I go. you are able to test for length of a string using this method

var='abcdef'
echo ${#var}

so this will test for a variable with the lengths inbetween 6 and 11

#!/bin/bash
echo "enter your password"
read PASS
if ((${#PASS} >= 6)) && ((${#PASS} <= 11))
    then echo "password has been accepted"    
fi

this will check if the lenght is between 6 and 11

((${#PASS} >= 6)) && ((${#PASS} <= 11))

this will check if you have at least one lowecase letter

[[ $VAR == *[a-z]* ]]

this will check if you have at least one uppercase letter

[[ $VAR == *[A-Z]* ]]

this will check if you have at least one number

[[ $VAR == *[1-9]* ]]

this will check if you have a letter at the end, either upper or lowercase

[[ $VAR == *[a-zA-Z] ]]

I am not sure(yet) how to determine if all of the chartacters are iether numbers, or letters, no special character. Example $%&^.

I will let you know what I figure out :-)

you can use this to check to see if your script has any characters other then numbers or letters

[[ ${VAR} == *[^a-bA-B1-9]* ]]

this will return a 0 if any non letters or numbers are found.

It would be very easy to combine all of these tests into a single script. I learned quite abit myself while looking for this info :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.