0

I am trying to create a password field and want to limit the user to use 8 or more characters for their password. Here is what I have in my code:

set /P "pass=Create Folder Password:"
if NOT "%pass%" GEQ 8 goto INVALID

I realized that the inequalities only work for numbers, but I was wondering if there is any way to use a similar method to check the length of the string


Aacini's Solution:

set /P "pass=Create Folder Password:
if "%pass:~10%" equ "" goto INVALID

This solution worked for me.

  • 1
    First you need the string length https://stackoverflow.com/questions/5837418/how-do-you-get-the-string-length-in-a-batch-file – Jerry Jeremiah Jan 18 '22 at 20:33
  • 3
    The simplest way is to test if the 8th character exists: `if "%pass:~7%" equ "" goto INVALID` This avoids to calculate the string lenght... – Aacini Jan 18 '22 at 20:52
  • 1
    The above commented example will determine whether you have at least eight characters, as requested. If you needed to detect if you had exactly eight characters, you could extend it to `If "%pass:~7%" == "" (GoTo INVALID) Else If Not "%pass:~8%" == "" GoTo INVALID`, and of course you could use an upper limit of lets say fourteen characters, like this: `If "%pass:~7%" == "" (GoTo INVALID) Else If Not "%pass:~14%" == "" GoTo INVALID`. – Compo Jan 18 '22 at 21:43
  • Do you want I post my comment as an answer? This way you may select it as Best Answer so other users know that this question was solved – Aacini Jan 21 '22 at 04:39

0 Answers0