Regex expression not working on Google Form

68 Views Asked by At

I'm trying to use a TextValidationBuilder to validate input on a Google Form. I'm doing some tests and I have no idea why the regex isn't working. When I test the regex here it works fine.

I want to use regex to validate that an input is a time or a number and I am doing this by checking if the format is something like "12:30PM" or "8:00AM" or "7."

Here is the regex: ([01]{0,1}\d:[0-6]\d((PM)|(AM)))|(\d+)

I'm putting the expression manually into the form to test. (I know programmatically I'll need to write \\d and whatnot.

The regex doesn't seem to work and allows the following:

  • "12:30" without the "AM" or "PM"
  • "1234:5678"

However, when I just try to validate for the time, it works correctly. Using [01]{0,1}\d:[0-6]\d((PM)|(AM)). It only fails when I add the or case for digits only: \d+.

If anyone could help me that would be super appreciated! Thank you!!

enter image description here

Edit: I'm just doing some troubleshooting and here are some things I noticed that are a bit odd:

When I match for the time or a random number like:

([01]{0,1}\d:[0-6]\d((PM)|(AM)))|1,

it works and doesn't accept responses like "12:30." But when I match for the time or multiple numbers like:

([01]{0,1}\d:[0-6]\d((PM)|(AM)))|[01],

it doesn't work and accepts "12:30."

1

There are 1 best solutions below

0
meelszz On

Thanks to @Nick in the comments, using anchors fixed my problem. Start the regex with ^ and end with $.