Solved Check if string doesn't contains letters or digits?

Hewlett-packard / Pavilion hpe
May 6, 2014 at 01:55:32
Specs: Windows 7 Professional, 3.6GHz 16GB
How would I check if a string contains numbers and/or letters only? I gave it a shot myself but I suspect that I am far off in the wrong direction:

@Echo off
setlocal enabledelayedexpansion
set "username=abc¤123_."
set "invalidChars=!""#¤%&/()=?`@£${[]>}´^~¨<*'_:;,.-"
for %%A in ("!username!") do (
find "%%A" "!invalidChars!"
if %errorlevel%==1 goto error
Echo s'all good
Echo invalid character found

I need this to prevent potential users from entering an invalid username string.

If infinity is infinite, is the square root of infinity infinite as well?
- Kurp Von Steiner

message edited by RainBawZ

See More: Check if string doesnt contains letters or digits?

Report •

May 6, 2014 at 19:56:43
✔ Best Answer
@echo off & setlocal enabledelayedexpansion
set ok=0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
set /p a=:
if not defined a goto :xxx
set c=%a:~0,1%
echo C:%c%
set test=!ok:%c%=!
echo test:[%test%]--ok:[%ok%]
if %test% equ %ok% goto :bad
set a=%a:~1%
echo ----------%a%
goto :1

echo chars not
goto :eof

echo PASSED ********************************
::====== end batch
best I could do, sorry!

message edited by nbrane

Report •

May 6, 2014 at 22:07:39
Hi nbrane,

I figured somebody would do this with findstr.

I have never been comfortable with findstr; not sure why.

Does [0-9a-z] cover UPPER & lower? [!]

M2 Golden-Triangle

Report •

May 7, 2014 at 17:49:55
Hi M2: Yeah, I started to go that route last night, and had a working version, but for some reason (too many beers?) I posted the other version. Findstr would not digest the whole word, however, but only in bytes (ha ha):
@echo off
:: limit character-set in this case 0-9 and a-z also A-Z
set z=[0-9a-z]
set ok=
set str=
set /p str=:
if not defined str goto :eof
set c=-1
set ok=ok
:: feed the "word" to findstr in byte-sized morsels
set /a c+=1
set k=!str:~%c%,1!
if not defined k goto :2
echo %k% | findstr /r /i %z%>nul||set ok=failed
goto :1
and for some reason it ignores case, although if in doubt you could always make it: [0-9a-zA-Z], or use the /I switch.

message edited by nbrane

Report •
Related Solutions

Ask Question