Home > Back-end >  Prevent multiple emails in one line in Google Forms using regex
Prevent multiple emails in one line in Google Forms using regex

Time:01-31

I have a long-form field ("Paragraph" type) in a Google Form. Users are expected to fill in any number of email addresses - at least one email, could be as many as 20-50 email addresses for some users.

I want to make sure that:

  1. Each line is likely to be a valid email (by checking for a "@" character and a "." character)

  2. Each line contains ONLY ONE email (by checking for "@" characters not separated by line breaks)

I know I can use the following string to check for two valid email addresses separated by a line break:

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

However, this limits the user to submitting two (no more, no less) email addresses.

Is there a way to check for 1 email address per line, and allow anything from 1 to multiple emil addresses?

CodePudding user response:

You could write the pattern with anchors and repeating 1 or more newlines followed by the same pattern.

^[\w. -] @[a-zA-Z0-9-] \.[a-zA-Z0-9.-] (?:\n [\w. -] @[a-zA-Z0-9-] \.[a-zA-Z0-9.-] )*$

See a regex101 demo

  • Related