unknown
unknown

Reputation: 1913

Put validation on user input in batch script

I need to put a validation when user enter the value he should enter only numeric value but the user will enter the values in below format

Echo Build Number in the Format 5.1.2.44
set /p Build="enter Build Number Please : "

If he puts the values in 5.1.2.3 then he needs to identify that it is numeric..he can use any character ?/., between the values instead of .(dot)

Valid examples are 5.1.2.3

Invalid examples are 5.2.A.3

Upvotes: 1

Views: 469

Answers (1)

David Ruhmann
David Ruhmann

Reputation: 11367

Here is an example of how to verify that each value between the delimiters is a number.

@echo off
set "xResult=valid"
for /f "tokens=1,2,3,4 delims=?/.," %%A in ("%Build%") do (
    for /f "tokens=1 delims=1234567890" %%n in ("%%A") do set "xResult=invalid"
    for /f "tokens=1 delims=1234567890" %%n in ("%%B") do set "xResult=invalid"
    for /f "tokens=1 delims=1234567890" %%n in ("%%C") do set "xResult=invalid"
    for /f "tokens=1 delims=1234567890" %%n in ("%%D") do set "xResult=invalid"
)
echo %xResult%

This works by first parsing the Build string into four separate values (A-D) by the delimiters (?/.,) and checking if each of those values are only comprised of the numbers (0-9).

Upvotes: 1

Related Questions