I recently created a contact form for my latest project bankrutt.se
I used a model for my contact entity and the view was auto-generated nicely.
One thing that struck me was that the form only validated if any text was entered, how about e-mail validation?
It seems that you would have to create your own regular expression validation using [RegularExpression(“####”)]
This seems fair enough but I am to lazy for that!
A quick google lead me to this blog.
Downloading the the GitHub package Install-Package DataAnnotationsExtensions.MVC3
DataAnnotationsExtensions.MVC3
You could with no effort achieve the same thing using the attribute
[Email]
(like in the example below)
The currently available validators are:
CreditCard Date Digits Email EqualTo FileExtensions Integer Max Min Numeric Url
public class Contact { [Required(ErrorMessage = "Du måste ange din e-postadress")] [Email(ErrorMessage="Felaktig e-postadress")] [DataType(DataType.EmailAddress)] public string From { get; set; } [Required(ErrorMessage = "Du måste ange rubrik")] public string Subject { get; set; } [DataType(DataType.MultilineText)] [Required(ErrorMessage = "Du måste skriva ett meddelande")] public string Message { get; set; } }
This is more how it should be!
This works for both server and client side validation as well.
Some of the extension validation attributes will be available in 4.5 (like Email, CreditCard, etc).