INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

alnum test

alnum test

(OP)

hello,
is this test ok - I need to check and be sure user name on a list for creation has only alphanumeric...

echo $X|awk '$1 !~ /^[[:alnum:]]+$/ {print "notalnum"}'

such test confirms but... is this test 100% ok?

CODE

$ for i in '$' '_' '%' '^' 1321 bsjs fkljs;do for y in 2786yqweughqk;do [[ -z `echo $y$i|awk '$1 !~ /^[[:alnum:]]+$/ {print "notalnum"}'` ]] && echo $y$i is alnum || echo $y$i is not alnum;done;done
2786yqweughqk$ is not alnum
2786yqweughqk_ is not alnum
2786yqweughqk% is not alnum
2786yqweughqk^ is not alnum
2786yqweughqk1321 is alnum
2786yqweughqkbsjs is alnum
2786yqweughqkfkljs is alnum
$ 

RE: alnum test

Hi

If the following should not output "notalnum" then Ok :

CODE --> command-line

echo 'foo _,-~"~-._' | awk '$1 !~ /^[[:alnum:]]+$/ {print "notalnum"}' 

Personally I would prefer grep, it is usually faster :

CODE

for i in '$' '_' '%' '^' '1321' 'bsjs' 'fkljs'; do
  for y in '2786yqweughqk'; do
    echo "$y$i" | grep -xqE '[[:alnum:]]+' && echo "$y$i is alnum" || echo "$y$i is not alnum"
  done
done 

Well, actually I would prefer this one, but works only in bash 3 or newer :

CODE

for i in '$' '_' '%' '^' '1321' 'bsjs' 'fkljs'; do
  for y in '2786yqweughqk'; do
    [[ "$y$i" =~ ^[[:alnum:]]+$ ]] && echo "$y$i is alnum" || echo "$y$i is not alnum"
  done
done 

Feherke.
http://feherke.github.com/

RE: alnum test

#!/bin/sh
# validalAlphaNum - Ensures that input only consists of alphabetical
#              and numeric characters.



CODE

validAlphaNum()
{
  # validate arg: returns 0 if all upper+lower+digits, 1 otherwise
  # Remove all unacceptable chars
  compressed="$(cat $1 | sed -e 's/[^[:alnum:]]//g')"
  if [ "$compressed" != `cat $1` ] ; then
   return 1
  else
   return 0
  fi
 }
# Sample usage of this function in a script
echo "Enter input: "
read input
if ! validAlphaNum "$input" ; 
 then
 echo "Your input must consist of only letters and numbers." >&2
 exit 1
else
 echo "Input is valid."
 fi
 exit 0 

Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close