Q:

validate user input in batch file

I have a batch file that asks the user for 2 inputs. A text file is then created/updated which includes the entries the user has entered. These entries need to be of a specific format.
i.e. (FIRST INPUT)
letter, letter, number number,space,number,letter,number,number,number, space letter,letter.
e.g. AB52 7L056 TA
(all letters need to be in upper case)
(SECOND INPUT)
Letter,letter,letter,number,letter,space,(then 11 numbers).
e.g. EBH9E 12323405432
I also need to limit the input so there is no space entered at the end of each input. My code so far is:
@echo off
@echo This is a test :> dblank.txt
SET /P someVar=[Please enter STRING and press ENTER]
@echo [THIS IS THE STRING ENTERED] %someVar% >> dblank.txt
@echo This is another TEST :>> dblank.txt
SET /P someVar2=[Please enter NEXT STRING and press ENTER]
@echo [THIS IS THE SECOND STRING ENTERED] %someVar2% >> dblank.txt
@echo DOES IT WORK ???>> dblank.txt

Could someone help with this?
Many thanks in advance.

A:

Well, it is very different to read any input from the user and then validate it (and repeat the process until the user enter the right input), than limit the input to a given format, so invalid characters are just not accepted. For the first method you may use findstr command as previously suggested.
Some time ago I wrote a Batch program that achieves precisely this type of formatted input. This is its help screen:
Read a line from keyboard with specific format

call :ReadFormattedLine var="mask"  [/M "message"]  [/P]  [/A|/W|/F]

The mask specify valid input characters per position via the following chars:

   #  -  Any digit
   _  -  Any letter
   +  -  A letter that is converted to uppercase
   ?  -  Any letter or digit
   @  -  Letter or digit, convert letter to uppercase

The following characters are just displayed/inserted at their positions:
   $  /  \  (  )  [  ]  :  ;  ,  .  -  space  letters  digits

Any character in the mask different than previous ones cause an error.

If /P (password) switch is given, input characters are displayed as asterisks.

Normally the input is completed when Enter key is pressed after read at least
one character, but the following switches changes this behavior.

   /A (auto):   Input is auto-completed after the last character; Enter key
                is ignored.
   /W (whole):  Enter key is accepted at first or last input positions only,
                that is, when input is empty or whole.
   /F (fields): Enter key fills the field with spaces and move the cursor to
                the next input field in the line.

To input a whole value terminated by Enter, use /W switch and insert any
character at the first position in the mask.

Using :ReadFormattedLine subroutine you may solve your problem this way:
call :ReadFormattedLine FirstInput="++## #+### ++" /M "First input: "
echo Input read: "%FirstInput%"

call :ReadFormattedLine SecondInput="+++#+ ###########" /M "Second input: "
echo Input read: "%SecondInput%"

You may download the subroutine from this site.

