【问题标题】:Validating Input in Batch File XY######## - Letters and Numbers Combination验证批处理文件中的输入 XY######## - 字母和数字组合
【发布时间】:2018-08-22 00:34:29
【问题描述】:

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

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

我自己更像是一个 Bash 人,所以我有点迷茫。

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

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

【问题讨论】:

  • 这可能会有所帮助:dostips.com/forum/viewtopic.php?t=5775
  • 对如何改进没有任何评论的拒绝投票与您不知道它们锁定什么的键一样有用。
  • @Squashman 看起来很有希望,我不认为它会那么复杂,但看起来可行。
  • 嗯,否决票按钮的气球帮助确实说“问题没有显示任何研究工作”。
  • 我将使用findstr 进行输入验证:findstr /I "^[A-Z][A-Z][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$"

标签: validation batch-file


【解决方案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 发布的链接是一个很好的例子,说明如何做到这一点......

【讨论】:

  • 我刚刚测试了这个,我喜欢它如何强制输入有效。也许在我考虑验证用户输入之前,我需要考虑强制输入有效。
  • 当用户尝试输入错误的字符时,它甚至会发出一点“哔”声。
  • 运行此代码时,我似乎无法复制并粘贴到终端中,有什么办法可以解决这个问题吗?
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-03
    • 2015-03-19
    • 1970-01-01
    相关资源
    最近更新 更多