Search code examples
regexgoogle-formsregex-lookaroundsregex-group

Regex for password validation at Google Forms


I am trying to create a regular expression to validate a password-like field in my Google Form. The problem I am facing is that Google Form's regex validation does not support lookaheads. It gives a "Please enter a valid regular expression" error. So I am stuck with creating a regex without lookaheads.

Only following two conditions must be fulfilled for my password validation regex:

  1. Length of characters must at least 9 characters
  2. Must have at least one number and at least one alphabet.

So, strings like password12, 12PASSWORD, pass12word, 12pasSWord12, pas12pas12pas12!!@@ etc all should be accepted by the regex.

I have currently tried using lookahead like the following expression which works well on other regex validation modules but gives an error on Google Form regex. Please look at this example 1
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,15}$

I tried to create my regex by using an IF clause like the following expression. Again this regex works well on other regex validations but gives an error in Google Form. Please see this example 2
\A(?>(?<char>[a-zA-Z])|(?<digit>[0-9])|.){8,}?((?(char)(?(digit)|(?!))|(?!)))


Now I can't use lookaheads and I also can't use IF conditions. So my guess is I am left with simple straightforward regex. I have tried to create several such regex but they work for specific kinds of password variations only.

For example, these following regex works there but they are incomplete:

(([0-9]+[a-zA-Z]*){9,}[a-zA-Z]+)|(([a-zA-Z]+[0-9]*){9,}[0-2]+) => This only validates strings like password12 or 12password
([a-zA-Z]*[0-9]*[a-zA-Z][0-9][a-zA-Z]*[0-9]*){9,} => This only validates p1p2p3p4p5p6p7

Can you help me in creating a similar regex to the above 2 regex'es which will satisfy the two conditions required(length>8 and at least one alphabet and digit)?

I would appreciate any help I can get. Thanks!


Solution

  • Apparently there is no simple solution for this. Here is a solution which takes into consideration all the possible variations of the string.

    .*([A-Za-z][0-9]......|[A-Za-z].[0-9].....|[A-Za-z]..[0-9]....|[A-Za-z]...[0-9]...|[A-Za-z]....[0-9]..|[A-Za-z].....[0-9].|[A-Za-z]......[0-9]|[0-9][A-Za-z]......|[0-9].[A-Za-z].....|[0-9]..[A-Za-z]....|[0-9]...[A-Za-z]...|[0-9]....[A-Za-z]..|[0-9].....[A-Za-z].|[0-9]......[A-Za-z]|.[A-Za-z][0-9].....|.[A-Za-z].[0-9]....|.[A-Za-z]..[0-9]...|.[A-Za-z]...[0-9]..|.[A-Za-z]....[0-9].|.[A-Za-z].....[0-9]|.[0-9][A-Za-z].....|.[0-9].[A-Za-z]....|.[0-9]..[A-Za-z]...|.[0-9]...[A-Za-z]..|.[0-9]....[A-Za-z].|.[0-9].....[A-Za-z]|..[A-Za-z][0-9]....|..[A-Za-z].[0-9]...|..[A-Za-z]..[0-9]..|..[A-Za-z]...[0-9].|..[A-Za-z]....[0-9]|..[0-9][A-Za-z]....|..[0-9].[A-Za-z]...|..[0-9]..[A-Za-z]..|..[0-9]...[A-Za-z].|..[0-9]....[A-Za-z]|...[A-Za-z][0-9]...|...[A-Za-z].[0-9]..|...[A-Za-z]..[0-9].|...[A-Za-z]...[0-9]|...[0-9][A-Za-z]...|...[0-9].[A-Za-z]..|...[0-9]..[A-Za-z].|...[0-9]...[A-Za-z]|....[A-Za-z][0-9]..|....[A-Za-z].[0-9].|....[A-Za-z]..[0-9]|....[0-9][A-Za-z]..|....[0-9].[A-Za-z].|....[0-9]..[A-Za-z]|.....[A-Za-z][0-9].|.....[A-Za-z].[0-9]|.....[0-9][A-Za-z].|.....[0-9].[A-Za-z]|......[A-Za-z][0-9]|......[0-9][A-Za-z]).*

    Visit this to test it out: https://regexr.com/5pimr