We help IT Professionals succeed at work.

HTML5 Form - MVC - require email format

375 Views
Last Modified: 2014-08-22
Hi:
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" })
                </div>

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.

RBS
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
RBS

Author

Commented:
Perfect - I understand - and thanks!

RBS
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.