Checking an argument in DOS

Program652
Program652 used Ask the Experts™
on
I am writing a DOS batch file that will take one argument, an SSN.  Is there a way that I can check the format of the argument?  Of course I want to insure that the argument is in the form of ###-##-####.
TIA!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Test your restores, not your backups...
Top Expert 2016
Commented:
Here's a simple routine that you could use in a BAT file to validate a SSN. It checks that the dashes are in the right place, that there are not more than 11 characters, and that each of the digits found are between 0 and 9.

@echo off
setlocal EnableDelayedExpansion
call :DoCheck "123-12-1234"
echo "%Valid%"
call :DoCheck "123121234"
echo "%Valid%"
call :DoCheck "23-12-12345"
echo "%Valid%"
call :DoCheck "ABC-12-1234"
echo "%Valid%"
call :DoCheck "000-99-0009"
echo "%Valid%"
call :DoCheck "000-99-00099"
echo "%Valid%"
exit /b

:DoCheck [ssn]
  set Valid=N
  set S=%~1
  if "%S:~3,1%" NEQ "-" exit /b
  if "%S:~6,1%" NEQ "-" exit /b
  if "%S:~11,1%" NEQ "" exit /b
  set S=%S:-=%
  for /L %%A in (0,1,8) do (
    if "!S:~%%A,1!" GTR "9" exit /b
    if "!S:~%%A,1!" LSS "0" exit /b
  )
  set Valid=Y
  exit /b

Open in new window

~bp

Author

Commented:
Made it look easy!  Thanks.
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Welcome.

~bp

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial