HTML5 Form - MVC - require email format

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
RBSAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
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:  http://www.regular-expressions.info/email.html
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RBSAuthor Commented:
Perfect - I understand - and thanks!

RBS
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Applications

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.