Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- :: REM - updated Version. Uses an Optional Secondary Parameter to Define What Character types to Permit.
- :: REM - Comprehensively tests input for the Desired Variable for Characters that would Cause Script failure prior
- :: REM - to restricting the variable to Character types Defined by an optional 2nd parameter
- @ECHO OFF
- ::: REM The below 6 lines of script allow testing of the function, and are not part of the function.
- ::: REM 2nd CALL parameter Limits input to numbers
- :start
- cls
- CALL :NameTypeLength option number
- ECHO %option%
- pause
- GOTO :start
- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Input Validation Function by T3RRY
- ::: Usage: Parameter 1 is Mandatory. Parameters 2 and 3 are optional.
- ::: When Needing User Input:
- :::
- ::: CALL :NameTypeLength <VarName> <PermitType> <MaxLength>
- ::: Mandatory: <VarName> is replaced with the Variable name to be Set
- ::: Recommended: <PermitType> is replaced with one of the following terms: letter number alphanumerical
- ::: Optional: <MaxLength> is The Maximum Allowed String length.
- :NameTypeLength
- (
- SETLOCAL EnableDelayedExpansion
- ::: Assign the variable (Passed by call Argument) to be set and how to test it
- SET "testinput=%~1"
- SET "permitted=%~2"
- SET "MaxLength=%~3"
- )
- ::: SYNTAX ::: Tests Parameter Usage is Correct
- IF NOT DEFINED testinput (
- ECHO([31mError. CALL :NameTypeLength Function Not used correctly. [36mParameter 1 for Variable Name is Required.
- ECHO([33m Parameter 2 for Character Types is Optional, though strongly [35mRecommended.
- ECHO([33m Parameter 3 for String Length is Optional.
- Pause
- Exit
- )
- IF NOT "!testinput!"=="!testinput: =!" (
- ECHO([31mError. CALL :NameTypeLength Parameter 1 : [33m%testinput% [31mis Not an acceptable Variable Name. Variable names Cannot Contain Spaces.
- Pause
- EXIT
- )
- REM Parameter 2 for Permitted Character types is Optional, However risks unwanted characters being input.
- IF DEFINED permitted Set "P_True=0"
- IF /I "!permitted!"=="number" Set /a P_True+=1
- IF /I "!permitted!"=="letter" Set /a P_True+=1
- IF /I "!permitted!"=="alphanumerical" Set /a P_True+=1
- IF "!P_True!"=="0" (
- ECHO([31mError. Incorrect Parameter used for CALL :NameTypeLength 2nd Parameter. %permitted% Is Not acceptable.
- ECHO([31mCorrect Parameters are: "[32mnumber[31m" OR "[32mletter[31m" OR "[32malphanumerical[31m"
- Pause
- Exit
- )
- REM Parameter 3 for MaxLength is Optional. The below Line ensures a value still exists for Later Usage.
- IF NOT DEFINED MaxLength Set "MaxLength=9999"
- echo.!MaxLength!| findstr /R "[^0-9]" >nul 2>nul
- IF NOT errorlevel 1 (
- ECHO([31mError. CALL :NameTypeLength Paramater 3 is Not Defined correctly. [33m%MaxLength% [31mneeds to be a [32mNumber
- Pause
- Exit
- )
- ::: Expand testinput to the Name of the Variable to be Assigned, Ensures Undefined
- SET %testinput%=
- ::: Ensure Undefined Value for Input
- SET input=
- :Get_Input
- ::: Exit Filter once Acceptable Variable is Set
- IF DEFINED input GOTO return
- ::: Create and start VBS script to Launch an Input Box and Store the input to text file for retrieval from this Batch program.
- SET /P "input=[Select %testinput%:]"
- ::: Test to ensure Variable defined. Needed here to prevent environment variable not defined message ahead of next test Should the variable not be defined.
- IF NOT DEFINED input GOTO invInput
- ::: Test for Doublequotes and reset variable if present
- SET Input | FIND """" >NUL
- IF NOT ERRORLEVEL 1 SET Input=
- IF NOT DEFINED input GOTO invInput
- :: REM Block Tilde
- SET Input | FIND "~" >NUL
- IF NOT ERRORLEVEL 1 SET Input=
- IF NOT DEFINED input GOTO invInput
- ::: Test for Spaces
- IF NOT "%input%"=="%input: =%" GOTO invInput
- ::: To permit symbols REM out permitted Symbol comparisons and Call Filter without Second Parameter.
- ::: Test for all other standard Symbols.
- IF NOT "%input%"=="%input:&=%" GOTO invInput
- IF NOT "%input%"=="%input:(=%" GOTO invInput
- IF NOT "%input%"=="%input:)=%" GOTO invInput
- IF NOT "%input%"=="%input:<=%" GOTO invInput
- IF NOT "%input%"=="%input:>=%" GOTO invInput
- IF NOT "%input%"=="%input:^=%" GOTO invInput
- IF NOT "%input%"=="%!!|%" GOTO invInput
- IF NOT "%input%"=="%input:{=%" GOTO invInput
- IF NOT "%input%"=="%input:}=%" GOTO invInput
- IF NOT "%input%"=="%input:?=%" GOTO invInput
- IF NOT "%input%"=="%input:!=%" GOTO invInput
- IF NOT "%input%"=="%input:`=%" GOTO invInput
- IF NOT "%input%"=="%input:'=%" GOTO invInput
- IF NOT "%input%"=="%input:]=%" GOTO invInput
- IF NOT "%input%"=="%input:[=%" GOTO invInput
- IF NOT "%input%"=="%input:#=%" GOTO invInput
- IF NOT "%input%"=="%input:+=%" GOTO invInput
- IF NOT "%input%"=="%input:-=%" GOTO invInput
- IF NOT "%input%"=="%input:/=%" GOTO invInput
- IF NOT "%input%"=="%input:\=%" GOTO invInput
- IF NOT "%input%"=="%input:$=%" GOTO invInput
- IF NOT "%input%"=="%input:@=%" GOTO invInput
- IF NOT "%input%"=="%input:,=%" GOTO invInput
- IF NOT "%input%"=="%input:.=%" GOTO invInput
- IF NOT "%input%"=="%!!%" GOTO invInput
- :: REM Length restriction. Optional 3rd parameter. If not used, MaxLength is set to 9999, effectively removing the Limit.
- IF NOT "!input:~%MaxLength%!"=="" (
- ECHO([33mlimit of %MaxLength% Characters allowed for %testinput%[32m
- Set input=
- GOTO :Get_Input
- )
- IF /I "%permitted%"=="number" (
- echo.!input!| findstr /R "[^0-9]" >nul 2>nul
- IF NOT errorlevel 1 (
- GOTO invInput
- ) else (
- GOTO :Get_Input
- )
- )
- IF /I "%permitted%"=="letter" (
- echo.!input!| findstr /R "[^a-zA-Z]" >nul 2>nul
- IF NOT errorlevel 1 (
- GOTO invInput
- ) else (
- GOTO :Get_Input
- )
- )
- IF /I "%permitted%"=="alphanumerical" (
- echo.!input!| findstr /R "[^a-zA-Z0-9]" >nul 2>nul
- IF NOT errorlevel 1 (
- GOTO invInput
- ) else (
- GOTO :Get_Input
- )
- )
- GOTO :Get_Input
- :invInput
- SET input=
- ::: REM Specifies the permitted input types.
- ECHO([33mInput of %permitted% characters are allowed. [31mOther Symbols and Characters are Not Permitted.[32m
- GOTO :Get_Input
- :return
- ::: assigns the input value to the variable name being validated.
- (
- ENDLOCAL & Set "%testinput%=%input%"
- )
- exit /b
- :::
- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: End Input Validation Function
Advertisement
Add Comment
Please, Sign In to add comment