batch file input numbers only

April 13, 2011 at 02:38:00
Specs: Windows XP
i had a batch file that need to input month, day and year my problem is

1.when i input letters still it will accept
2. when i input space still it will accept

please help me to put a restriction that will not accept letters and spaces i need your expertise.


SET /P deyt1=Type the Month ^<MM^> :
if not "%deyt1:~2%"=="" (
echo. ERROR: Name cannot be more than 2 chars
goto START
)
IF NOT DEFINED deyt1 goto START

SET /P deyt2=Type the Day ^<DD^> :
if not "%deyt2:~2%"=="" (
echo. ERROR: Name cannot be more than 2 chars
goto START
)
IF NOT DEFINED deyt2 goto START

SET /P deyt3=Type the Year ^<YYYY^>:
if not "%deyt3:~4%"=="" (
echo. ERROR: Name cannot be more than 4 chars
goto START
)
IF NOT DEFINED deyt3 goto START


See More: batch file input numbers only

Report •


#1
April 13, 2011 at 11:03:05
@echo off
:month
cls
set /p month=Type the Month ^<MM^>:
if %month% < 1 goto day
if %month% > 12 goto day
//this will make sure your entry is 1 - 12
goto month

:day
cls
set /p day=Type the Day ^<DD^>:
if %day% < 1 goto day
if %day% > 31 goto day
//this will make sure your entry is 1 - 31
goto year

:year
cls
set /p year=Type the Year ^<YYYY^>:
if %year% < 1980 goto deyt3
if %year% > 2030 goto deyt3
//this will make sure your entry is 1980 - 2030
//you might want to dhange the year range
goto finish

:finish
cls
echo %month%/%day%/%year%
pause >nul
exit

This will let you type in a number then It'll make sure it's within a certain range. Then it'll move on to the next one until the finish room were It'll display the date.


Report •

#2
April 13, 2011 at 19:18:55
@bbman225

Your code doesn't work, here is a few things you should know:

[1]Set /p can return undefined variables.
[2]Batch "IF" uses 3 letter codes for numeric comparison(lss, gtr,geq,leq,equ and neq)
which will also compare ASCII codes of letters.
[3] set /p returns whole lines and standard %vars% can execute code and cause syntax errors(mainly in the if statements) if you don't handle them carefully.
[4]There isn't 31 days in every month.

====================================
It isn't pretty but here is my stab at it:

Edit: Minor change.

@echo off
SetLocal

:invaliddate

call :numeric_input month 2 "Type the Month <MM> :"
call :numeric_input day 2 "Type the Day <DD> :"
call :numeric_input Year 4 "Type the Year <YYYY> :"

call :isvaliddate %year% %month% %day%
if %validdate%==false (
    echo ERROR: Invalid date
    goto invaliddate
)
echo %year% %month% %day%
pause


goto :eof
:isvaliddate
rem call :isvaliddate year month day
set validdate=false
if "%~1"=="" goto :eof
if "%~2"=="" goto :eof
if "%~3"=="" goto :eof
for /f "tokens=1 delims=0123456789" %%a in ("%~1%~2%~3") do if not "%%a"=="" goto :eof
for /f "tokens=1* delims=0" %%a in ("A0%~1") do if "%%b"=="" (goto :eof) else set val_year=%%b
for /f "tokens=1* delims=0" %%a in ("A0%~2") do if "%%b"=="" (goto :eof) else set val_mon=%%b
for /f "tokens=1* delims=0" %%a in ("A0%~3") do if "%%b"=="" (goto :eof) else set val_day=%%b
if %val_mon% gtr 12 goto :eof
set validdate=true
for %%a in (1 3 5 7 8 10 12) do if %val_mon%==%%a if %val_day% gtr 31 set validdate=false
for %%a in (4 6 9 11) do if %val_mon%==%%a if %val_day% gtr 30 set validdate=false
if '!!==' (set esc=^^) else set esc=
set /a leap="(%esc%!(val_year %% 100) * -1) + %esc%!(val_year %% 400) + %esc%!(val_year %% 4)"
if %val_mon%==2 (
    if %leap%==1 (
        if %val_day% gtr 29 set validdate=false
    ) else if %val_day% gtr 28 set validdate=false
)
goto :eof

:numeric_input
rem call :numeric_input variable maxlen "string"
set %1=
set /p "%1=%~3"
if not defined %1 (
    echo ERROR: You must enter a number
    goto numeric_input
)

for /f "delims=" %%a in (' call echo:^"%%%1:"=%%%"') do (
    for /f "tokens=1* delims=0123456789" %%b in ("A0%%~a") do (
        if not "%%~c"=="" (
            echo ERROR: Numerical input only
            goto numeric_input
        )
    )
)
for /f "tokens=1* delims=0" %%a in (' call echo:A0%%%1:^"^=%%%') do (
    if "%%~b"=="" (
        echo ERROR: The Input must be greater than 0
        goto numeric_input
    ) else set %1=%%b
)
for /f "tokens=1* delims=X" %%a in (' call echo AX%%%1:~%2%%%') do (
    if not "%%~b"=="" (
        echo ERROR: Input must be less than or equal to %2 characters
        goto numeric_input
    )
)
goto :eof


Report •

#3
April 14, 2011 at 00:02:43
Thank you Sir :)

Report •

Related Solutions

#4
April 14, 2011 at 00:56:08
@judago

it works thank you for the help :)


Report •

#5
April 14, 2011 at 00:58:51
No problem, I'm glad I could help!

Report •

#6
April 14, 2011 at 01:58:08
@Judago

When i try to input in Month greater than 12 ex. 13 still it will accept. how will i limit the input to 12 months only?

Thanks in advance


Report •

#7
April 14, 2011 at 02:08:30
@judago

please disregard my question.


Report •

#8
April 14, 2011 at 02:56:02
Experts cannot reply within the minute, some of them check their reply's within hours or days.

Be patient.

Happy is ONE who says I am an OttoMAN.


Report •

#9
April 14, 2011 at 03:39:41
@beautiful stranger

I'm assuming that you wanted me disregard because it checks the date is valid after the input...

The input routine only checks that the input is numerical, greater than zero and less than or equal to the number of characters specified. After the input the date is passed off to another routine to check the date is a valid date, I did it this way to make the code clearer, especially for 29 feb because it needs the year to check.

To be honest I edited in the month check about ~2 minutes after I initially posted(before you replied), if you copied it before then just copy it again ;).


Report •

#10
April 25, 2011 at 20:19:23
i'm referring to disregard my question dated April 14, 2011 at 01:58:08. the reason why i comment to disregard because when i try again to input a Month greater than 12 ex. 13 it will not accept.

I'm really sorry for the confusion

Thank you (^^,)


Report •

Ask Question