Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

Does anyone know how I can use a "@" within the pattern attribute on a HTML5 email input having a MVC page?

<input type="email" pattern="^[a-zA-Z0-9._+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$">

At runtime I get this error:

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

When I use "data-pattern" instead of "pattern" I can escape it with "@@", but with "pattern" that fails too.

Thanks in advance :)

share|improve this question
up vote 8 down vote accepted

It can be done 2 ways:

Render the "@" through razor:

<input type="email" pattern="^[a-zA-Z0-9._+-]+@("@")[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$">

With a HTML encode:

<input type="email" pattern="^[a-zA-Z0-9._+-]+&#64;[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$">

Thanks go out to my backend development colleague Tim :)

share|improve this answer
    
Great stuff, not sure why this hasn't got more upvotes! – Adam Marshall Dec 16 '13 at 11:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.