T3RRYT3RR0R

Batch Set /p Validation Alpha and or Numerical

Jan 9th, 2020
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 6.26 KB | None | 0 0
  1. :: REM - updated Version. Uses an Optional Secondary Parameter to Define What Character types to Permit.
  2. :: REM - Comprehensively tests input for the Desired Variable for Characters that would Cause Script failure prior
  3. :: REM - to restricting the variable to Character types Defined by an optional 2nd parameter
  4.  
  5. @ECHO OFF
  6. ::: REM The below 6 lines of script allow testing of the function, and are not part of the function.
  7. ::: REM 2nd CALL parameter Limits input to numbers
  8.  
  9. :start
  10. cls
  11. CALL :NameTypeLength option number
  12. ECHO %option%
  13. pause
  14. GOTO :start
  15.  
  16. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Input Validation Function by T3RRY
  17. ::: Usage: Parameter 1 is Mandatory. Parameters 2 and 3 are optional.
  18. ::: When Needing User Input:
  19. :::
  20. ::: CALL :NameTypeLength <VarName> <PermitType> <MaxLength>
  21. ::: Mandatory:      <VarName>    is replaced with the Variable name to be Set
  22. ::: Recommended:    <PermitType> is replaced with one of the following terms: letter number alphanumerical
  23. ::: Optional:       <MaxLength>  is The Maximum Allowed String length.
  24.  
  25. :NameTypeLength
  26. (
  27. SETLOCAL EnableDelayedExpansion
  28. ::: Assign the variable (Passed by call Argument) to be set and how to test it
  29. SET "testinput=%~1"
  30. SET "permitted=%~2"
  31. SET "MaxLength=%~3"
  32. )
  33.  
  34. ::: SYNTAX ::: Tests Parameter Usage is Correct
  35.  
  36.     IF NOT DEFINED testinput (
  37.         ECHO(Error. CALL :NameTypeLength Function Not used correctly. Parameter 1 for Variable Name is Required.
  38.         ECHO( Parameter 2 for Character Types is Optional, though strongly Recommended.
  39.         ECHO( Parameter 3 for String Length is Optional.
  40.         Pause
  41.         Exit
  42.     )
  43.    
  44.     IF NOT "!testinput!"=="!testinput: =!" (
  45.         ECHO(Error. CALL :NameTypeLength Parameter 1 : %testinput% is Not an acceptable Variable Name. Variable names Cannot Contain Spaces.
  46.         Pause
  47.         EXIT
  48.     )
  49.  
  50.     REM Parameter 2 for Permitted Character types is Optional, However risks unwanted characters being input.
  51.     IF DEFINED permitted Set "P_True=0"
  52.     IF /I "!permitted!"=="number" Set /a P_True+=1
  53.     IF /I "!permitted!"=="letter" Set /a P_True+=1
  54.     IF /I "!permitted!"=="alphanumerical" Set /a P_True+=1
  55.    
  56.     IF "!P_True!"=="0" (
  57.         ECHO(Error. Incorrect Parameter used for CALL :NameTypeLength 2nd Parameter. %permitted% Is Not acceptable.
  58.         ECHO(Correct Parameters are: "number" OR "letter" OR "alphanumerical"
  59.         Pause
  60.         Exit
  61.     )
  62.  
  63.     REM Parameter 3 for MaxLength is Optional. The below Line ensures a value still exists for Later Usage.
  64.     IF NOT DEFINED MaxLength Set "MaxLength=9999"
  65.    
  66.     echo.!MaxLength!| findstr /R "[^0-9]" >nul 2>nul
  67.     IF NOT errorlevel 1 (
  68.         ECHO(Error. CALL :NameTypeLength Paramater 3 is Not Defined correctly. %MaxLength% needs to be a Number
  69.         Pause
  70.         Exit
  71.     )
  72.  
  73. ::: Expand testinput to the Name of the Variable to be Assigned, Ensures Undefined
  74.  
  75. SET %testinput%=
  76.  
  77. ::: Ensure Undefined Value for Input
  78.  
  79. SET input=
  80.  
  81. :Get_Input
  82.  
  83. ::: Exit Filter once Acceptable Variable is Set
  84.  
  85. IF DEFINED input GOTO return
  86.  
  87. ::: Create and start VBS script to Launch an Input Box and Store the input to text file for retrieval from this Batch program.
  88.  
  89. SET /P "input=[Select %testinput%:]"
  90.  
  91. ::: Test to ensure Variable defined. Needed here to prevent environment variable not defined message ahead of next test Should the variable not be defined.
  92.  
  93. IF NOT DEFINED input GOTO invInput
  94.  
  95. ::: Test for Doublequotes and reset variable if present
  96.  
  97. SET Input | FIND """" >NUL
  98. IF NOT ERRORLEVEL 1 SET Input=
  99.  
  100. IF NOT DEFINED input GOTO invInput
  101.  
  102. :: REM Block Tilde
  103.  
  104. SET Input | FIND "~" >NUL
  105. IF NOT ERRORLEVEL 1 SET Input=
  106.  
  107. IF NOT DEFINED input GOTO invInput
  108.  
  109. ::: Test for Spaces
  110.  
  111. IF NOT "%input%"=="%input: =%" GOTO invInput
  112.  
  113. ::: To permit symbols REM out permitted Symbol comparisons and Call Filter without Second Parameter.
  114. ::: Test for all other standard Symbols.
  115.  
  116. IF NOT "%input%"=="%input:&=%" GOTO invInput
  117. IF NOT "%input%"=="%input:(=%" GOTO invInput
  118. IF NOT "%input%"=="%input:)=%" GOTO invInput
  119. IF NOT "%input%"=="%input:<=%" GOTO invInput
  120. IF NOT "%input%"=="%input:>=%" GOTO invInput
  121. IF NOT "%input%"=="%input:^=%" GOTO invInput
  122. IF NOT "%input%"=="%!!|%" GOTO invInput
  123. IF NOT "%input%"=="%input:{=%" GOTO invInput
  124. IF NOT "%input%"=="%input:}=%" GOTO invInput
  125. IF NOT "%input%"=="%input:?=%" GOTO invInput
  126. IF NOT "%input%"=="%input:!=%" GOTO invInput
  127. IF NOT "%input%"=="%input:`=%" GOTO invInput
  128. IF NOT "%input%"=="%input:'=%" GOTO invInput
  129.  
  130. IF NOT "%input%"=="%input:]=%" GOTO invInput
  131. IF NOT "%input%"=="%input:[=%" GOTO invInput
  132. IF NOT "%input%"=="%input:#=%" GOTO invInput
  133. IF NOT "%input%"=="%input:+=%" GOTO invInput
  134. IF NOT "%input%"=="%input:-=%" GOTO invInput
  135. IF NOT "%input%"=="%input:/=%" GOTO invInput
  136. IF NOT "%input%"=="%input:\=%" GOTO invInput
  137. IF NOT "%input%"=="%input:$=%" GOTO invInput
  138. IF NOT "%input%"=="%input:@=%" GOTO invInput
  139. IF NOT "%input%"=="%input:,=%" GOTO invInput
  140. IF NOT "%input%"=="%input:.=%" GOTO invInput
  141. IF NOT "%input%"=="%!!%" GOTO invInput
  142.  
  143. :: REM Length restriction. Optional 3rd parameter. If not used, MaxLength is set to 9999, effectively removing the Limit.
  144.  
  145.     IF NOT "!input:~%MaxLength%!"=="" (
  146.         ECHO(limit of %MaxLength% Characters allowed for %testinput%
  147.         Set input=
  148.         GOTO :Get_Input
  149.     )
  150.  
  151.     IF /I "%permitted%"=="number" (
  152.         echo.!input!| findstr /R "[^0-9]" >nul 2>nul
  153.         IF NOT errorlevel 1 (
  154.             GOTO invInput
  155.         ) else (
  156.             GOTO :Get_Input
  157.         )
  158.     )
  159.    
  160.     IF /I "%permitted%"=="letter" (
  161.         echo.!input!| findstr /R "[^a-zA-Z]" >nul 2>nul
  162.         IF NOT errorlevel 1 (
  163.             GOTO invInput
  164.         ) else (
  165.             GOTO :Get_Input
  166.         )
  167.     )
  168.    
  169.     IF /I "%permitted%"=="alphanumerical" (
  170.         echo.!input!| findstr /R "[^a-zA-Z0-9]" >nul 2>nul
  171.         IF NOT errorlevel 1 (
  172.             GOTO invInput
  173.         ) else (
  174.             GOTO :Get_Input
  175.         )
  176.     )
  177.    
  178.     GOTO :Get_Input
  179.    
  180. :invInput
  181.  
  182. SET input=
  183.  
  184. ::: REM Specifies the permitted input types.
  185.  
  186. ECHO(Input of %permitted% characters are allowed. Other Symbols and Characters are Not Permitted.
  187.  
  188. GOTO :Get_Input
  189.  
  190. :return
  191.  
  192. ::: assigns the input value to the variable name being validated.
  193.  
  194. (
  195. ENDLOCAL & Set "%testinput%=%input%"
  196. )
  197.  
  198. exit /b
  199.  
  200. :::
  201. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: End Input Validation Function
Advertisement
Add Comment
Please, Sign In to add comment