Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hey guys, This is my first question so hopefully it will turn out ok.
Basically, i've been creating a batch file for few days now. It's basically a database where you can add, view and delete records from a text file.
My problem is, i want to restrict the user input to only text or only numbers.For example. When i go to add it says this.
Type Firstname:I want to restrict that part to Text only. And if they enter a number, after they press the key enter i want it to display a message saying wring input or something..
Don't worry, i know how to do everything else except just restricting it..Heres the code
set /P firstname=Type Firstname: %=%
All i need is the code after that bit to restrict to Text only, or to dis allow numbers & symbols.
This is my idea. (it doesn't work, but it's what i've tried and failed. you should be able to understand where i'm coming from.)
set /P firstname=Type Firstname: %=%
IF %firstname% == 0-9 msg * lalalalado you get me ? i want something like that.
I also want to know how to restrict to number only, as i have more inputs after firstname.Thanks
Matt

To check what typed is not an easy job in batch script. The following is close to do the job, BUT it allows special symbols. To know more about findstr type findsrr /? at prompt.
:LOOP1 [Leters only] set /P firstname=Enter FirstName^> echo.%firstname% | findstr /R /C:[0-9] > nul if not ErrorLevel 1 ( echo. ERROR: "%firstname%" contains numbers goto :LOOP1 ) :LOOP2 [Numbers only] set /P onlynumbers=Enter Numbers^> echo.%onlynumbers% | findstr /R /C:[A-Z] > nul if not ErrorLevel 1 ( echo. ERROR: "%onlynumbers%" contains letters goto :LOOP2 )

if you want to do it with batch, you can use findstr with regular expression to filter what you want from user input.
however, this sort of thing like what you are doing is easily done using a programming language that have good functions to do what you want. here's an example in Python ( of course, if you know vbscript, it also have filter functions )
firstname=raw_input("Type Firstname: ") if firstname.isalpha(): print "ok" else: print "not ok"
outputC:\test>python test.py Type Firstname: fFsdfs ok C:\test>python test.py Type Firstname: sdf23 not ok

Thanks for the help guys, I'll try adding the code later on. i should manage to get this working.
Matt

bin a while since i used error level codec :L
so this will come up wiv error for one number charactor inputs..if u are really desperate then you will do all number digits until u finish the code :P otherwise ur a quitterif you type a single number up comes error, if just ur name then it will say hello (then ur name here)
:start
set /p firstname=
if %firstname% == 0 goto error
if %firstname% == 1 goto error
if %firstname% == 2 goto error
if %firstname% == 3 goto error
if %firstname% == 4 goto error
if %firstname% == 5 goto error
if %firstname% == 6 goto error
if %firstname% == 7 goto error
if %firstname% == 8 goto error
if %firstname% == 9 goto error
echo hello %firstname%
pause
exit
:error
echo Error watever thingeh
pause
goto start

What about:
set /P firstname=Type Firstname: %=%
set "test=%firstname%"
for %%a in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do if defined test call set "test=%%test:%%a=%%"
if defined test (
echo The input contained characters that are not contained in a-z
)

"and add another 26 chars for upper case in your for loop?"
Nope, %variable:string=substitute% is case insensitive in most respects, the only respect that it isn't is that the case of the substitute string is preserved.

judago
Yours looks promising :P
Thanks for the help guys. Judago's code was the type i was looking for.Matt

Ok, another reply. I tried your idea Judago, but it didn't work for me. I may have had something wrong i don't know.
But, the good news is that i tried IVO's idea. & it worked perfect first time..
Thanks IVO, you have helped me a lot.
Thanks to the other guys who posted for trying too.Matt

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |