验证批处理文件中的输入XY ########-字母和数字组合

时间:2018-08-22 00:34:30

标签: validation batch-file

我已经创建了一个可以正确执行的批处理/ cmd文件,但是我想添加一个“验证”层来检查输入是否正确。

输入的格式应为LETTER-LETTER-########(八个数字)

我本人更是一个Bash人,所以我有点迷茫。

这是我正在使用的基本版本。

echo Please Input like so XY########
set /P INPUT=Type input: %=%

2 个答案:

答案 0 :(得分:1)

好的。我们去...

如果通过set /P命令读取输入,则无法做您想做的事情。在这种情况下,您可以稍后再测试输入,然后重复输入直到正确为止。

要在输入时同时检查输入内容,您需要分别阅读和测试每个字符。有几种方法可以做到这一点。最简单的一种基于choice命令:

@echo off
setlocal EnableDelayedExpansion

echo Please Input like so XY########
set /P "=Type input: " < NUL
set "INPUT="

rem Get two *UPPERCASE* letters
set "letter= ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for /L %%i in (1,1,2) do (
   choice /C %letter% /N > NUL
   for %%l in ("!errorlevel!") do set "INPUT=!INPUT!!letter:~%%~l,1!"
   set /P "=!INPUT:~-1!" < NUL
)

rem Get eight digits
set "digit= 0123456789"
for /L %%i in (1,1,8) do (
   choice /C %digit% /N > NUL
   for %%l in ("!errorlevel!") do set "INPUT=!INPUT!!digit:~%%~l,1!"
   set /P "=!INPUT:~-1!" < NUL
)

echo/
echo INPUT = "%INPUT%"

在此代码中:

  • 任何按下的字母都将转换为大写字母。可以取消此行为,包括在letter变量中使用大写和小写字母,并在/CS命令中添加choice开关。
  • 不能删除最后输入的字符。
  • 输入最后一个字符后,输入将自动完成。不需要最终的ENTER键。

如果您不希望出现这种情况,并且需要对输入字符进行更精确的控制,则必须使用另一种方法。例如,通过xcopy技巧读取密钥。 Squashman上面发布的链接是有关如何执行此操作的充分示例...

答案 1 :(得分:0)

Aacini的答案还要求有效的用户输入,而不是验证追溯的有效性,他的答案是更少的代码和更多的“可读性” IMO。我将使用他的答案,但我想我会分享这种方法。

我要求“追溯”验证用户输入,但是@Squashmans带注释的URL首先强制使用有效的用户输入。

https://www.dostips.com/forum/viewtopic.php?t=5775

这是我最后得到的“经过消毒的代码”。

@echo off
@cls

::::START -- Section Blocks User from Inputting Invalid Data::::
setlocal
set "thisFile=%~F0"

call :ReadFormattedLine INPUT="__########" /M "Enter Input in Form XY########: "
echo/

:ReadFormattedLine var="mask" [/M "message"] [/P] [/F /W /A]

if "%~2" equ "" echo ERROR: Missing parameters & exit /B 1
setlocal EnableDelayedExpansion

set "var=%~1"
set "mask=%~2"
shift & shift
set "message="
if /I "%1" equ "/M" set "message=%~2" & shift & shift
set "password="
if /I "%1" equ "/P" set "password=1" & shift
set "switch=%~1"

set quote="
set "digit= 0 1 2 3 4 5 6 7 8 9 "
set "letter= 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 "
set "alphaNum=%digit%%letter%"
set "fmt=#_+?@"
set "show=$/\()[]:;,.- %digit: =%%letter: =%"
for /F %%a in ('copy /Z "%thisFile%" NUL') do set "CR=%%a"
for /F %%a in ('echo prompt $H ^| cmd') do set "BS=%%a" & set "SP=.%%a "

< NUL (
   set /P "=%message%"
   for /F "eol=| delims=" %%a in ('cmd /U /C echo !mask!^| find /V ""') do (
      if "!fmt:%%a=!" neq "%fmt%" (
         set /P "=Û"
      ) else if "%%a" neq " " (
         set /P "=%%a"
      ) else (
         set /P "=!SP!"
      )
   )
   set /P "=!SP!!CR!%message%"
)
set "input="
set /A i=0, key=0
goto checkFormat

:nextKey
   set "key="
   for /F "delims=" %%a in ('xcopy /W "%thisFile%" "%thisFile%" 2^>NUL') do if not defined key set "key=%%a"
   if "!key:~-1!" neq "!CR!" goto endif
      if /I "%switch%" equ "/A" goto nextKey
      if /I "%switch%" neq "/F" goto check/W
         :nextField
         set "format=!mask:~%i%,1!"
         if "%format%" equ "" goto endRead
         if "!fmt:%format%=!" equ "%fmt%" goto checkFormat
         set /P "=Û" < NUL
         set "input=%input% "
         set /A i+=1
         goto nextField
      :check/W
      if /I "%switch%" neq "/W" goto checkEmpty
         if %i% equ 0 goto endRead
         if "%format%" equ "" goto endRead
         goto nextKey
      :checkEmpty
      if %i% gtr 0 goto endRead
      goto nextKey
   :endif
   set "key=!key:~-1!"
   if "!key!" equ "!BS!" (
      if %i% gtr 0 (
         if "%format%" equ "" (
            set /P "=!SP!!BS!!BS!Û!BS!" < NUL
         ) else (
            set /P "=Û!BS!!BS!Û!BS!" < NUL
         )
         set "input=%input:~0,-1%"
         set /A i-=1
         if !i! equ 0 set key=0
      )
      goto checkFormat
   )
   if "%format%" equ "" goto nextKey
   if "!key!" equ "=" goto nextKey
   if "!key!" equ "!quote!" goto nextKey
   if "%format%" equ "#" ( rem Any digit
      if "!digit: %key% =!" equ "%digit%" goto nextKey
   ) else if "%format%" equ "_" ( rem Any letter
      if "!letter: %key% =!" equ "%letter%" goto nextKey
   ) else if "%format%" equ "+" ( rem Any letter, convert it to uppercase
      if "!letter: %key% =!" equ "%letter%" goto nextKey
      for %%a in (%letter%) do if /I "!key!" equ "%%a" set "key=%%a"
   ) else (
      rem Rest of formats are alphanumeric: ? @
      if "!alphaNum: %key% =!" equ "%alphaNum%" goto nextKey
      if "%format%" equ "@" ( rem Convert letters to uppercase
         for %%a in (%letter%) do if /I "!key!" equ "%%a" set "key=%%a"
      ) else if "%format%" neq "?" echo ERROR: Invalid format in mask: "%format%" & exit /B 2
      )
   )
   if defined password (
      set /P "=*" < NUL
   ) else (
      set /P "=%key%" < NUL
   )
   set "input=%input%%key%"

   :nextFormat
   set /A i+=1
   :checkFormat
   set "format=!mask:~%i%,1!"
   if "%format%" equ "" (
      if /I "%switch%" equ "/A" goto endRead
      if /I "%switch%" equ "/M" goto endRead
      goto nextKey
   )
   if "!show:%format%=!" neq "%show%" (
      if "!key!" equ "!BS!" (
         if "%format%" neq " " (
            set /P "=%format%!BS!!BS!Û!BS!" < NUL
         ) else (
            set /P "=!SP!!BS!!BS!Û!BS!" < NUL
         )
         set "input=%input:~0,-1%"
         set /A i-=1
         if !i! equ 0 set key=0
         goto checkFormat
      ) else (
         if "%format%" neq " " (
            set /P "=%format%" < NUL
         ) else (
            set /P "=!SP!" < NUL
         )
         set "input=%input%%format%"
         goto nextFormat
      )
   )
   if "%input:~-1%!key!" equ " !BS!" (
      set /P "=Û!BS!!BS!" < NUL
      set "input=%input:~0,-1%"
      set /A i-=1
      goto checkFormat
   )

goto nextKey
:endRead
echo/
endlocal & set "%var%=%input%"
echo %INPUT%
pause
exit /B