HTML5 Form - MVC - require email format

Posted on 2014-08-22
Last Modified: 2014-08-22
I have an MVC5 app in which I create a ViewModel to populate a form.  I would like to require that one of the fields have a valid email address.  I have decorated the relevant field as follows in the ViewModel:

[Required, MaxLength(100), DataType(DataType.EmailAddress), Display(Name="Email From")]
        public string EmailFrom { get; set; }

Open in new window

The required field is written as follows in the chtml page:

                <div class="control-group">
                    @Html.LabelFor(model => model.EmailFrom, new { @class = "control-label" })
                    @Html.TextBoxFor(model => model.EmailFrom, null, new { @class = "help-inline form-control" })
                    @Html.ValidationMessageFor(model => model.EmailFrom, null, new { @class = "help-inline" })

Open in new window

The code works as far as requiring something to be entered - but lets me enter a non-valid email field.  any help in fixing this would be greatly appreciated.

Question by:RBS
    LVL 74

    Accepted Solution

    So long as you understand that you are only validating structure, not that an email actually exists, this should suffice:

    [Required, MaxLength(100), DataType(DataType.EmailAddress), Display(Name="Email From"), RegularExpression(@"(?i)^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$")]
    public string EmailFrom { get; set; }

    Open in new window

    The above includes a RegularExpression data annotation that uses a slight modification of the pattern found at:

    Author Closing Comment

    Perfect - I understand - and thanks!


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
    Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now