String Validation program

Hi

I want to validate the sting which one having only A-Z, a-z, *, . ,_ and 0-9 digits. Can anyone send me the program? I tried with following program but its taking all special characters like @ , # % and ^.

echo " Enter Text :"
read text
while [ ! $(echo "$text" | tr -dc '[A-za-z0-9]') ]
do
echo "Character is wrong"
done

Do you know about POSIX classes - maybe something using tr -<whatever> [:alnum:]

[A-z] lets some punctutation like ^ "through"

Plus, unless you turn off globbing, the * character will not come through as *, it will come through as a listing of files in the current directory.

I had this script for sometime. Check it out.

[/tmp]$ cat string.ksh 
#! /bin/ksh 

set -f

(($#)) && STR="$@" || STR="abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()-=_+[]{}"
STRING="$STR"
typeset -R1 last
typeset -L1 first

while ((${#STR})) 
do
        first="${STR}"
        last="${STR}"

        [[ "$first" = [a-zA-Z0-9\.\_\*] ]] || { echo "Unwanted $first" ; exit 1 ; }
        [[ "$last" = [a-zA-Z0-9\.\_\*] ]] || { echo "Unwanted $last" ; exit 1 ; }

        STR="${STR%$last}"
        STR="${STR#$first}"

        ((${#STR} == 1)) && break;

done
        echo "$STRING looks fine."

Like jim has said, you need to turn off globbing or invoke the script as

sh -c 'set -f; ./string.ksh abcd@#$'

where abcd@#$ is the string you are trying to validate. All characters you wish to keep should go inside the [a-zA-Z0-9\.\_\*] construct.

Hi,
Thanx to all...

The given program is working fine but typeset -R and -L values are not working in linux machine. Is there any other variables to declare this typeset for string in linux?

Regards
mpk

Linux isn't the problem, it's your shell. Some linux distros use pdksh but this supports typeset -L and -R as well.