Test email with regular expression via Firebase Firestore security rule

Viewed 106

I am working on a contract form hosted on Firebase. User can submit a form without authorization (anonymous authorization only). I have client side data validation on form but i struggle to test email with regular expression via Firestore security rule on the server side. Here is what i have in RE2 syntax:
allow create: if request.resource.data.email.matches("/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,}$/i");
I have tried without "/", also does not work. Following regex works perfectly for telephone number, however:
allow create: if request.resource.data.tel.matches("^[+][0-9]{9,}$");
Thanks in advance!
1 Answers

You can use

allow create: if request.resource.data.email.matches('^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$')

That is, you should pass the regex as a string literal, not as a regex literal enclosed within a string literal.

Since in this case you cannot use /i flag you need to add a-z to your pattern.

If it is a RE2 pattern, you may also use a (?i) case insensitive inline modifier:

'(?i)^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,}$'
Related