Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validation Issue 2

Status
Not open for further replies.

SteveR77

Programmer
Sep 18, 2000
813
US
In the following code the line stating

if ! validAlphaNum $input ; then is never seen to do the evaluation. Can someone tell me what I have missed.


#!/bin/sh

# Ensures that input is only alphanumeric


validAlphaNum()
{

# Validate arg: returns 0 for success & 1 for failure

# Strip all unacceptable characters

compressed="$(echo $1 | sed -e 's/[^[:alnum:]]//g')"

if [ "$compressed" != "$input" ] ; then
return 1
else
return 0
fi
}

echo "Enter input: "
read input

if ! validAlphaNum $input ; then
echo "Your input must consist of only letters & numbers." >&2
exit 1
else
echo "Input is valid."
fi

exit 0
~
 
Have you tried something like this ?
validAlphaNum $input || {
echo "Your input must consist of only letters & numbers." >&2
exit 1
}
echo "Input is valid."

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks PH.

It's been a long day. I was thinking of case logic but knew that would not be the best way to try and get this to work.

Steve
 
PH,


Changing the test condition to what you posted still yields the same result. If it is set up as an OR as you have shown the expression always executes the false and if it is an AND it always evaluates to be true.

I know I am missing something simple with this one.


Thanks in advance to everyone for their assistance.
 
You may consider some debugging, like a set -x in the body of the function to trace it.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
SteveR77,

I think this should work fine:

Code:
#!/bin/ksh

# Ensures that input is only alphanumeric
echo "Enter input: "
read input

C1=$(echo $input | sed -e 's/[^[:alnum:]]//g' | wc -c )
C2=$(echo $input | wc -c )

if [ $C1 -ne $C2 ]; then
  echo "Your input must consist of only letters & numbers." >&2
   exit 1
else
  echo "Input is valid."
fi

exit 0

John
 
Steve,

Using PHV's suggestion and changing $1 to $input, this works as well:

Code:
#!/bin/sh

# Ensures that input is only alphanumeric


validAlphaNum()
 {

# Validate arg: returns 0 for success & 1 for failure

# Strip all unacceptable characters

compressed="$(echo $input | sed -e 's/[^[:alnum:]]//g')"

if [ "$compressed" != "$input" ] ; then
   return 1
else
  return 0
fi
}

echo  "Enter input: "
read input

validAlphaNum $input || {
  echo "Your input must consist of only letters & numbers." >&2
  exit 1
}
echo "Input is valid."

John
 
PHV & John,

Thanks. There was something out of wack with the POSIX being understood. When I hard coded the regular expression values for the range of alphanumeric characters it worked.

This one was no fun because I knew it should work. Now that I know what is wrong I also have the logic for the case statement and nawk tests to work as well.

Note to self - ALWAYS check the environment prior to testing the logic.

Thanks once again gents. I think this one would make a good FAQ for validation of data.


Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top