Restrict the character user input

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
xhai
Posts: 39
Joined: 13 Jun 2013 09:33

Restrict the character user input

#1 Post by xhai » 09 Feb 2014 09:13

I found a code but i want to Restrict the character user input, is this possible and add extra codes..

:LOOP1 [Letters Only]
:LOOP2 [numbers Only]
:LOOP3 [Letters small]
:LOOP4 [Letters big]
:LOOP5 [all character]
:LOOP6 [non special character]

Code: Select all

:LOOP1 [Letters Only]
set firstname=
set /P firstname=Type Firstname: %=%
echo.%firstname% | findstr /R /C:[0-9] > nul
if not ErrorLevel 1 (
echo. ERROR: Name cannot contain numbers.
goto :LOOP1
)

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Restrict the character user input

#2 Post by Squashman » 09 Feb 2014 09:47

The square brackets in the findstr command is the key to your answer.
Read the help from the command prompt or at this link.
ss64.com/nt/findstr.html

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Restrict the character user input

#3 Post by foxidrive » 09 Feb 2014 15:53

This should solve much of what you need quite efficiently.

Code: Select all

@ECHO OFF
setlocal enabledelayedexpansion
:start
set "input="
set /p input=Enter Input: "
call :LOOP1
if defined notletters echo Not all letters
call :LOOP2
if defined notnumbers echo Not all numbers
call :LOOP3
if defined notsmall echo Not all lower case
call :LOOP4
if defined notbig echo Not all upper case
goto :start
goto :EOF

:LOOP1 [Letters Only]
set "notletters="
for /f "delims=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" %%a in ("%input%") DO set notletters=1
goto :EOF
:LOOP2 [numbers Only]
set "notnumbers="
for /f "delims=1234567890" %%a in ("%input%") DO set notnumbers=1
goto :EOF
:LOOP3 [Letters small]
set "notsmall="
for /f "delims=abcdefghijklmnopqrstuvwxyz" %%a in ("%input%") DO set notsmall=1
goto :EOF
:LOOP4 [Letters big]
set "notbig="
for /f "delims=ABCDEFGHIJKLMNOPQRSTUVWXYZ" %%a in ("%input%") DO set notbig=1
goto :EOF


Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Restrict the character user input

#4 Post by Squashman » 09 Feb 2014 19:32

I like that idea Foxidrive.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Restrict the character user input

#5 Post by foxidrive » 09 Feb 2014 20:03

Ta. I still laugh because after using NT batch since XP days I only realised last year that the delims were case sensitive. :D

xhai
Posts: 39
Joined: 13 Jun 2013 09:33

Re: Restrict the character user input

#6 Post by xhai » 10 Feb 2014 20:06

foxidrive wrote:foxidrive


thanks it work.. can i use those for in here..

Code: Select all

:Compress
echo.
Set eCompress=No
echo Do you want to Compress the Patched ISO to CSO? (Y)es or (N)o
echo.
set /p compress=
if "%compress%"=="" goto start
if "%compress%"=="y" set eCompress=Yes
if "%compress%"=="Y" set eCompress=Yes
if "%compress%"=="n" set eCompress=No
if "%compress%"=="N" set eCompress=No


i couldn't make the user input restrict if user type asassasas it should go to :Start....
also about special character these could also be restrict..
only small/big Y or N are valid else it goes all at the :start

thank you, i hope you can fix this

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: Restrict the character user input

#7 Post by carlos » 10 Feb 2014 23:01

add goto :Start at end of ifs
also, you can use if /i for indicate case insensitive.

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Restrict the character user input

#8 Post by ShadowThief » 11 Feb 2014 07:29

xhai wrote:
foxidrive wrote:foxidrive


thanks it work.. can i use those for in here..

Code: Select all

:Compress
echo.
Set eCompress=No
echo Do you want to Compress the Patched ISO to CSO? (Y)es or (N)o
echo.
set /p compress=
if "%compress%"=="" goto start
if "%compress%"=="y" set eCompress=Yes
if "%compress%"=="Y" set eCompress=Yes
if "%compress%"=="n" set eCompress=No
if "%compress%"=="N" set eCompress=No


i couldn't make the user input restrict if user type asassasas it should go to :Start....
also about special character these could also be restrict..
only small/big Y or N are valid else it goes all at the :start

thank you, i hope you can fix this

What operating system are you using? If you're using Vista or later, you can use the choice command.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Restrict the character user input

#9 Post by Squashman » 11 Feb 2014 08:50

Whenever I have a yes no question I will do this to capture just the first letter just in case they type YES or type NO when all I really want is Y or N.

Code: Select all

:_answer
cls
Set /p "answer=Do you want to do this, Y or N:"
IF NOT "%answer%"=="" SET answer=%answer:~0,1%
IF /I "%answer%"=="N" Goto Label1
IF /I "%answer%"=="Y" Goto Label2
goto _answer

xhai
Posts: 39
Joined: 13 Jun 2013 09:33

Re: Restrict the character user input

#10 Post by xhai » 11 Feb 2014 09:06

What operating system are you using? If you're using Vista or later, you can use the choice command.


Window 7 32bit, now my problem is only special character

Squashman wrote:Whenever I have a yes no question I will do this to capture just the first letter just in case they type YES or type NO when all I really want is Y or N.

Code: Select all

:_answer
cls
Set /p "answer=Do you want to do this, Y or N:"
IF NOT "%answer%"=="" SET answer=%answer:~0,1%
IF /I "%answer%"=="N" Goto Label1
IF /I "%answer%"=="Y" Goto Label2
goto _answer


What does "~0,1%" do? if i edit 1 to 2 what will happen

thanks you all for helping me..

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Restrict the character user input

#11 Post by Squashman » 11 Feb 2014 12:07

xhai wrote:What does "~0,1%" do? if i edit 1 to 2 what will happen

Well how do you think you could test that? Make the change and echo the new value back to the screen to see what it does.

Post Reply