Batch file input restriction

June 10, 2009 at 08:10:55
Specs: Microsoft Windows XP Home Edition, 2.793 GHz / 502 MB
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 * lalalala

do 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.


See More: Batch file input restriction

Report •

June 10, 2009 at 09:22:15
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

Report •

June 10, 2009 at 09:24:51
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"
    print "not ok"

Type Firstname: fFsdfs

Type Firstname: sdf23
not ok

Report •

June 12, 2009 at 06:21:57
Thanks for the help guys, I'll try adding the code later on. i should manage to get this working.

Report •

Related Solutions

June 14, 2009 at 14:13:36
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 quitter

if you type a single number up comes error, if just ur name then it will say hello (then ur name here)

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%
echo Error watever thingeh
goto start

Report •

June 15, 2009 at 00:39:49
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

Report •

June 15, 2009 at 01:18:34
and add another 26 chars for upper case in your for loop?

Report •

June 15, 2009 at 03:13:27
"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.

Report •

June 16, 2009 at 02:17:11

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


Report •

June 16, 2009 at 06:53:52
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.


Report •

Ask Question