Scala Play: Invalid '@' symbol in email regex

910 views Asked by At

I'm trying to put an input regex that uses the pattern attribute to insure a valid username (the ^[a-zA-Z0-9_]{6,12}$ portion in the regex's below) OR email is entered in my Scala Play template. With Play templates, the @ is used to indicate a break from HTML and that "Scala goes here", so when I have my input line and try to include the @ used in emails. It complains:

Invalid '@' symbol

I've tried:

  1. Doing it normally
  2. Triple quotes, no escape character before the @
  3. One escape character before the @
  4. Two escape characters before the @ (with and without triple quotes)

    pattern="^[a-zA-Z0-9_]{6,12}$|/^[a-zA-Z0-9.!#$%&’+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:.[a-zA-Z0-9-]+)$/"

    pattern="""^[a-zA-Z0-9_]{6,12}$|/^[a-zA-Z0-9.!#$%&’+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:.[a-zA-Z0-9-]+)$/"""

    pattern=^[a-zA-Z0-9_]{6,12}$|/^[a-zA-Z0-9.!#$%&’+/=?^_`{|}~-]+\@[a-zA-Z0-9-]+(?:.[a-zA-Z0-9-]+)$/"

    pattern="^[a-zA-Z0-9_]{6,12}$|/^[a-zA-Z0-9.!#$%&’+/=?^_`{|}~-]+\@[a-zA-Z0-9-]+(?:.[a-zA-Z0-9-]+)$/"

Any ideas?

(EDIT Ugh Sorry, SO won't show stuff wrapped in input tags, and it also won't let me format the above as code, so I had to add just the pattern="..." after my original post without formatting as such, apologies.)

1

There are 1 answers

0
Wiktor Stribiżew On BEST ANSWER

Escaping @ in Scala Play template can be performed by doubling the symbol.

Use @@ to introduce a literal @ into the pattern.

However, since you are using a regex here, note that a hexadecimal \u0040 or \x40 might also be used (but only when the \ is a literal, i.e. it should be escaped for a regex engine, like """\u0040""", so that Scala does not parse it as a Unicode char).