URL
The System.ComponentModel.DataAnnotations.UrlAttribute maps to the url validation method in jQuery Validation.
Here's the model, note that the Url attribute decorates the sole property on the model:
using System.ComponentModel.DataAnnotations;
namespace jQuery.Validation.Unobtrusive.Native.Demos.Models
{
public class UrlModel
{
[Url]
public string Url { get; set; }
}
}
Here's the view (which uses the model):
@model jQuery.Validation.Unobtrusive.Native.Demos.Models.UrlModel
@using (Html.BeginForm())
{
<div class="row">
@Html.LabelFor(x => x.Url, "Only urls can be entered:")
@Html.TextBoxFor(x => x.Url, 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/Url" method="post">
<div class="row">
<label for="Url">Only urls can be entered:</label>
<input data-msg-url="The Url field is not a valid fully-qualified http, https, or ftp URL." data-rule-url="true"
id="Url" name="Url" 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!");
}
});