Batch File input validation - Make sure user entered an integer

前端 未结 16 2237
抹茶落季
抹茶落季 2020-12-03 14:33

I\'m experimenting with a Windows batch file to perform a simple operation which requires the user to enter a non-negative integer. I\'m using simple batch-file techniques t

相关标签:
16条回答
  • 2020-12-03 15:19

    You're probably not doing this in a DOS batch file. Or at least, support for set /p is unheard of for me in DOS :-)

    You could use substrings. In fact I have written a parser for a specific regular language that way once, but it's cumbersome. The easiest way would probably be to assign the contents of %userinput% to another variable, using set /a. If the result comes out as 0 you need to check whether the input itself was 0, otherwise you can conclude it was a non-number:

    @echo off
    setlocal enableextensions enabledelayedexpansion
    set /p UserInput=Enter a number: 
    set /a Test=UserInput
    if !Test! EQU 0 (
      if !UserInput! EQU 0 (
        echo Number
      ) else (
        echo Not a number
      )
    ) else (
      echo Number
    )
    

    However, this works only for numbers in the range of Int32. If you just care for any number (possibly floating-point as well) then you need to resort to the loop-based approach of dissecting it.

    NOTE: Updated to solve the space issues. However, there is still a problem lurking: Entering 123/5 yields "number", since set /a can evaluate this ...

    0 讨论(0)
  • 2020-12-03 15:20
    :ASK
    SET /P number= Choose a number [1 or 2]: 
    IF %number% EQU 1 GOTO ONE
    IF %number% NEQ 1 (
      IF %number% EQU 2 GOTO TWO
      IF %number% NEQ 2 (
        CLS
        ECHO You need to choose a NUMBER: 1 OR 2.
        ECHO.
        GOTO ASK
      )
    )
    

    It works fine to me. If he chooses numbers less or greater, strings, floating number etc, he wil receive a message ("You need to choose a NUMBER: 1 OR 2.") and the INPUT will be asked again.

    0 讨论(0)
  • 2020-12-03 15:24

    you can reinvent the wheel and grow a few white hairs doing string validation in batch, or you can use vbscript

    strInput = WScript.Arguments.Item(0)
    If IsNumeric(strInput) Then
        WScript.Echo "1"
    Else
        WScript.Echo "0"
    End If
    

    save it as checkdigit.vbs and in your batch

    @echo off
    for /F %%A in ('cscript //nologo checkdigit.vbs 100') do (
            echo %%A
            rem use if to check whether its 1 or 0 and carry on from here
    )
    
    0 讨论(0)
  • 2020-12-03 15:26

    I know this is years old, but just to share my solution.

    set /p inp=Int Only : 
    :: Check for multiple zeros eg : 00000 ::
    set ch2=%inp%-0
    if %inp% EQU 0 goto :pass
    if [%inp%]==[] echo Missing value && goto :eof
    if %inp:~0,1%==- echo No negative integers! && goto :eof
    set /a chk=%inp%-10>nul
    if %chk%==-10 echo Integers only! && goto :eof
    :pass
    echo You shall pass
    :eof
    

    Tested and working on Windows 8.

    0 讨论(0)
  • 2020-12-03 15:29

    You can also use a quite simple trick:

    echo %userinput%|findstr /r /c:"^[0-9][0-9]*$" >nul
    if errorlevel 1 (echo not a number) else (echo number)
    

    This uses findstr's regular expression matching capabilities. They aren't very impressive but useful at times.

    0 讨论(0)
  • 2020-12-03 15:31

    You can validate any variable if its number:

    SET "var="&for /f "delims=0123456789" %i in ("%a") do set var=%i
    if defined var (echo."NIC">nul) else (echo."number")
    
    0 讨论(0)
提交回复
热议问题