Computing.Net > Forums > Programming > Batch file input restriction

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch file input restriction

Reply to Message Icon

Name: Matt (by Mattio)
Date: June 10, 2009 at 08:10:55 Pacific
OS: Microsoft Windows XP Home Edition
CPU/Ram: 2.793 GHz / 502 MB
Product: Dell / Dell dv051
Subcategory: Batch
Comment:

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.

Thanks
Matt



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: June 10, 2009 at 09:22:15 Pacific
Reply:

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
)


0

Response Number 2
Name: ghostdog
Date: June 10, 2009 at 09:24:51 Pacific
Reply:

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"

output
C:\test>python test.py
Type Firstname: fFsdfs
ok

C:\test>python test.py
Type Firstname: sdf23
not ok


0

Response Number 3
Name: Matt (by Mattio)
Date: June 12, 2009 at 06:21:57 Pacific
Reply:

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


0

Response Number 4
Name: RobJohnB95
Date: June 14, 2009 at 14:13:36 Pacific
Reply:

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)

: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


0

Response Number 5
Name: Judago
Date: June 15, 2009 at 00:39:49 Pacific
Reply:

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
)

0

Related Posts

See More



Response Number 6
Name: ghostdog
Date: June 15, 2009 at 01:18:34 Pacific
Reply:

and add another 26 chars for upper case in your for loop?


0

Response Number 7
Name: Judago
Date: June 15, 2009 at 03:13:27 Pacific
Reply:

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


0

Response Number 8
Name: Matt (by Mattio)
Date: June 16, 2009 at 02:17:11 Pacific
Reply:

judago

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

Matt


0

Response Number 9
Name: Matt (by Mattio)
Date: June 16, 2009 at 06:53:52 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch file input restriction

Batch file input for logevent.exe www.computing.net/answers/programming/batch-file-input-for-logeventexe/15380.html

batch file input www.computing.net/answers/programming/batch-file-input/11258.html

Batch File|Input from txt file www.computing.net/answers/programming/batch-fileinput-from-txt-file-/17293.html