Batch File input validation - Make sure user entered an integer

前端 未结 16 2236
抹茶落季
抹茶落季 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:09

    Thanks all. I was trying to make it harder for myself looking at loops and string manipulation. I used your tips on math evaluation and comparison. Here's what I finally came up with as my concept script:

    :Top
    @ECHO OFF
    ECHO.
    ECHO ---------------------------------------
    SET /P UserInput=Please Enter a Number: 
    ECHO.
    ECHO UserInput = %UserInput%
    ECHO.
    SET /A Evaluated=UserInput
    ECHO Math-Evaluated UserInput = %Evaluated%
    if %Evaluated% EQU %UserInput% (
        ECHO Integer
        IF %UserInput% GTR 0 ( ECHO Positive )
        IF %UserInput% LSS 0 ( ECHO Negative )
        IF %UserInput% EQU 0 ( ECHO Zero )
        REM - Other Comparison operators for numbers
        REM - LEQ - Less Than or Equal To
        REM - GEQ - Greater Than or Equal To
        REM - NEQ - Not Equal To
    ) ELSE (
        REM - Non-numbers and decimal numbers get kicked out here
        ECHO Non-Integer
    )
    
    GOTO Top
    

    This method catches all numbers and can detect whether it's positive, negative, or zero. Any decimal or string will be detected as non-integers. The only edge case I've found is a string with spaces. For example, the text "Number 1" will cause the script to crash/close when the user input is evaluated as math. But in my situation, this is fine. I don't want my script to go on with invalid input.

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

    @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 )

    yeaph everthing is great but you forget about one little thing 0 also is a digit ;(

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

    Try this:

    set /p numeric=enter a number
    
    ( 
      (if errorlevel %numeric% break ) 2>nul
    )&&(
      echo %numeric% is numeric
    )||(
      echo %numeric% is NOT numeric
    )
    
    0 讨论(0)
  • 2020-12-03 15:15

    As pointed out by ghostdog74, the answers posted by Joey Mar 26 '09 (score 10) and Wouter van Nifterick Mar 26 '09 (score 5) don't work.

    The answer posted by Joey Mar 25 '10 (score 2) does work, except that redirection symbols and '&' cause syntax errors.

    I think the best and simplest solution is the one posted by Sager Oct 8 '14 (score 0). Unfortunately, it has a typo: ‘"%a"’ should be ‘"%a%"’.

    Here's a batch file based on Sager's answer. Redirection symbols and '&' in the input don't cause problems. The only problems I could find were caused by strings containing double quotes.

    @echo off & setlocal enableextensions & echo.
    set /p input=Enter a string:
    SET "x=" & for /f "delims=0123456789" %%i in ("%input%") do set x=%%i
    if defined x (echo Non-numeral: "%x:~0,1%") else (echo No non-numerals)
    
    0 讨论(0)
  • 2020-12-03 15:16

    This is more of a user friendly way.

    if %userinput%==0 (
    cls
    goto (put place here)
    )
    if %userinput%==1 (
    cls
    goto (put place here)
    )
    if %userinput%==2 (
    cls
    goto (put place here)
    )
    if %userinput%==3 (
    cls
    goto (put place here)
    )
    if %userinput%==4 (
    cls
    goto (put place here)
    )
    if %userinput%==5 (
    cls
    goto (put place here)
    )if %userinput%==6 (
    cls
    goto (put place here)
    )if %userinput%==7 (
    cls
    goto (put place here)
    )
    if %userinput%==8 (
    cls
    goto (put place here)
    )
    if %userinput%==9 (
    cls
    goto (put place here)
    )
    

    This can be used for any type of user input.

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

    In addition to the remark about the error that occures when spaces are part of the users input. You can use errorlevel errorlevel=9165. It can be used for the spaces in a string or for the error handling of 'no' input.

    Kind Regards,

    Egbert

    0 讨论(0)
提交回复
热议问题