Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ::# :ValidNum Function for accepting input of a positive number, with arguments to declare
- ::# return var, Maximum and Minimum ranges. Scripted to be Proof against code injection.
- ::# Blocks entry of: non integer strings, negative numbers, hex, octal, numbers with leading 0's and 0 literal,
- ::# and Integers outside of the declared range.
- @Echo off
- Rem usage examples:
- Rem Example 1 - Return var $RV, Max 31, default Min: 1
- Call:ValidNum $RV 31
- Rem Example 2 - Return var $RV2, Max 20, Min: 10
- Call:ValidNum $RV2 20 10
- Rem Example 3 - Max default: 2147483647, Min 5, return var default: $Num
- Call:ValidNum "" "" 5
- Set $
- Goto:Eof
- :ValidNum [returnvar] [max] [min]
- Rem - Forces positive numeric input within min max range.
- SETLOCAL
- Set "sign=-1"
- :VNumIn
- %= ensure nul value =%
- Set "input="
- %!! min integer =%
- 2> nul Set /a "min=%~3","1/min","1/(sign-(min>>31))","1/(%~2/%~3)" || Set "min=1"
- %!! max integer =%
- 2> nul Set /a "max=%~2","1/max","1/(sign-(max>>31))","1/(%~2/min)" || Set "max=2147483647"
- %= Prompt for input =%
- Set /p "input=Enter a number GEQ %min% LEQ %max%: "
- %= Any Input surviving the Below Set /A testing is safe for expansion =%
- %= Input Testing. input +/- , input > min , input < max , Hex/Octal for comparison =%
- 2>nul Set /a "1/(sign-(input>>31))","max/(input/min)","1/(max/input)","HexOct=input" || Goto:VNumIn
- %= compare assignments to block input of hex, octal or numbers with leading 0's =%
- If not "%Input%"=="%HexOct%" Goto:VNumIn
- ( %= return value in Arg1 if used ; else return in $Num =%
- ENDLOCAL & Set "%~1=%input%" 2> nul || Set "$Num=%input%"
- Goto:Eof
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement