Skip to content

Instantly share code, notes, and snippets.

@brossetti1
Last active August 1, 2024 17:58
Show Gist options
  • Save brossetti1/5741a422b1d5fe9324ddba3f6cfa1293 to your computer and use it in GitHub Desktop.
Save brossetti1/5741a422b1d5fe9324ddba3f6cfa1293 to your computer and use it in GitHub Desktop.
useful regex expressions

https://www.labnol.org/internet/regular-expressions-forms/28380/

Postal Address

[a-zA-Z\d\s\-\,\#\.\+]+

allow only alphanumeric characters, spaces and few other characters like comma, period and hash symbol in the form input field.

ZIP Code

^\d{5,6}(?:[-\s]\d{4})?$

the regex allows ZIP codes in standard formats and it matches both US and Indian pincodes.

Date

((0[1-9])|(1[0-2]))[\/-]((0[1-9])|(1[0-9])|(2[0-9])|(3[0-1]))[\/-](\d{4})

accept date input in the mm/dd/yyyy or mm-dd-yyyy formats. Also see: Get Google Form Data by Email

Email Address

[a-zA-Z0-9_\.\+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-\.]+

the regex below should match most common email address formats, including Gmail aliases that accept the “+” sign but there’s no perfect solution.

URL (Web domain)

https?\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,} https?\:\/\/(www\.)?youtu(\.)?be(\.com)?\/.*(\?v=|\/v\/)?[a-zA-Z0-9_\-]+

this is useful for fields that require the user to enter their website address and it even matches the upcoming TLDs like .directory or .restaurant. The other regex matches YouTube URL including those using the youtu.be domains.

Character Limit

[\w]{1,140}

the default text box in a Google form allows users to input any number of characters but you can impose a limit with the help of regular expression. Here we limit the input to 140 characters much like Twitter.

Phone Numbers

\+?\(?\d{2,4}\)?[\d\s-]{3,}

these are often a series of numbers preceded by an optional “+” sign and the area code may be inside brackets.

Price (with decimal)

\$?\d{1,3}(,?\d{3})*(\.\d{1,2})?

if a form field requires users to enter a price of an item in their own currency, this regex will help. Replace the $ sign with your own currency symbol.

Complex Password

(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9].*[0-9])(?=.*[^a-zA-Z0-9]).{8,}

only accept a string that has 1 uppercase alphabet, 1 lowercase alphabet, 2 digits and 1 special character. Also the minimum allowed length is 8 characters.

CAPTCHA

^(4|[Ff][Oo][Uu][Rr])$ - what is 2+2?

Google forms do not offer CAPTCHAs but you can create one using regex. Here’s a simple captcha that requires users to answer a simple question. Also see: Regular Expressions for Gmail Search

Word Limit

^[-\w]+(?:\W+[-\w]+){9,14}\W*$

If you would like to limit the number of words that a user can type in the input field of a Google Form, there’s a regex for that as well. In this case, we only allow any input that has between 10 to 15 words:

@shepos
Copy link

shepos commented Mar 16, 2022

it seems the expression for "Complex Password" is not a valid expression (in Forms), and CAPTCHA doesnt work (in Forms)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment