3

I would like to add an pattern for my email input field:

<input type="email" size="1" required="" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,63}$">

escaping with @@ does not work work

<input type="email" size="1" required="" pattern="[a-z0-9._%+-]+@@[a-z0-9.-]+\.[a-z]{2,63}$">

This does always give me the following exception:

"[" is not valid at the start of a code block. Only identifiers, keywords, comments, "(" and "{" are valid.

please note that the input is within a @if { } block.

Any idea what I can do?

Community
  • 1
  • 1
Bgl86
  • 727
  • 8
  • 20
  • Are you sure you need to escape it in this case? We do this all the time (admittedly I don't recall if it's in an `@if` block) but we don't need to escape the "@" in those cases. – Craig W. Jun 03 '16 at 14:34
  • You can use the html encoding `@` as it mentions here http://stackoverflow.com/questions/3626250/escape-character-in-razor-view-engine/13584640#13584640 – Electric Sheep Jun 03 '16 at 14:38
  • @Craig if I dont escape it, I am getting the same exception. – Bgl86 Jun 03 '16 at 14:41

1 Answers1

4

Parentheses to the rescue! Any time there’s an ambiguity in Razor, you can use parentheses to be explicit about what you want:

<input type="email" size="1" required="" pattern="[a-z0-9._%+-]+@("@")[a-z0-9.-]+\.[a-z]{2,63}$">
Mike Debela
  • 1,930
  • 3
  • 18
  • 32