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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

alnum test

Status
Not open for further replies.

w5000

Technical User
Nov 24, 2010
223
PL

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
$

 
Hi

If the following should not output "notalnum" then Ok :
Code:
echo [green][i]'foo _,-~"~-._'[/i][/green] [teal]|[/teal] awk [green][i]'$1 !~ /^[[:alnum:]]+$/ {print "notalnum"}'[/i][/green]

Personally I would prefer [tt]grep[/tt], it is usually faster :
Code:
[b]for[/b] i [b]in[/b] [green][i]'$'[/i][/green] [green][i]'_'[/i][/green] [green][i]'%'[/i][/green] [green][i]'^'[/i][/green] [green][i]'1321'[/i][/green] [green][i]'bsjs'[/i][/green] [green][i]'fkljs'[/i][/green][teal];[/teal] [b]do[/b]
  [b]for[/b] y [b]in[/b] [green][i]'2786yqweughqk'[/i][/green][teal];[/teal] [b]do[/b]
    echo [green][i]"$y$i"[/i][/green] [teal]|[/teal] grep -xqE [green][i]'[[:alnum:]]+'[/i][/green] [teal]&&[/teal] echo [green][i]"$y$i is alnum"[/i][/green] [teal]||[/teal] echo [green][i]"$y$i is not alnum"[/i][/green]
  [b]done[/b]
[b]done[/b]

Well, actually I would prefer this one, but works only in [tt]bash[/tt] 3 or newer :
Code:
[b]for[/b] i [b]in[/b] [green][i]'$'[/i][/green] [green][i]'_'[/i][/green] [green][i]'%'[/i][/green] [green][i]'^'[/i][/green] [green][i]'1321'[/i][/green] [green][i]'bsjs'[/i][/green] [green][i]'fkljs'[/i][/green][teal];[/teal] [b]do[/b]
  [b]for[/b] y [b]in[/b] [green][i]'2786yqweughqk'[/i][/green][teal];[/teal] [b]do[/b]
    [teal][[[/teal] [green][i]"$y$i"[/i][/green] [teal]=~[/teal] [teal]^[[:[/teal]alnum[teal]:]]+[/teal]$ [teal]]][/teal] [teal]&&[/teal] echo [green][i]"$y$i is alnum"[/i][/green] [teal]||[/teal] echo [green][i]"$y$i is not alnum"[/i][/green]
  [b]done[/b]
[b]done[/b]

Feherke.
[link feherke.github.com/][/url]
 
#!/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."
 
Anyway, why using external programs like sed, awk, ...
Code:
y=2786yqweughqk;for i in \$ _ % \^ 1321 bsjs fkljs;do case $y$i in *[!0-9A-Za-z]*)echo $y$i is not alnum;;*) echo $y$i is alnum;;esac;done
But, as mentioned by feherke, you have to clearly explain what YOU think is alnum ...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top