The System.ComponentModel.DataAnnotations.EmailAddressAttribute maps to the email validation method in jQuery Validation.
Here's the model, note that the EmailAddress attribute decorates the sole property on the model:
using System.ComponentModel.DataAnnotations;
namespace jQuery.Validation.Unobtrusive.Native.Demos.Models
{
public class EmailModel
{
[EmailAddress]
public string Email { get; set; }
}
}
Here's the view (which uses the model):
@model jQuery.Validation.Unobtrusive.Native.Demos.Models.EmailModel
@using (Html.BeginForm())
{
<div class="row">
@Html.LabelFor(x => x.Email, "Only an email address can be entered:")
@Html.TextBoxFor(x => x.Email, true)
</div>
<div class="row">
<button type="submit" class="btn btn-default">Submit</button>
<button type="reset" class="btn btn-info">Reset</button>
</div>
}
Here's the HTML that the view generates:
<form action="/Demo/Email" method="post">
<div class="row">
<label for="Email">Only an email address can be entered:</label>
<input data-msg-email="The Email field is not a valid e-mail address." data-rule-email="true"
id="Email" name="Email" type="text" value="" />
</div>
<div class="row">
<button type="submit" class="btn btn-default">Submit</button>
<button type="reset" class="btn btn-info">Reset</button>
</div>
</form>
Here's the JavaScript that initialises the validation:
$("form").validate({
submitHandler: function (form) {
alert("This is a valid form!");
}
});