我已使用ComponentModel.DataAnnotations
[必需]属性来验证消息。邮件未显示在我的表单中。但是,RegularExpression验证和范围验证工作正常。
在我的一个领域,我使用了[必需]和[RegualarExpression]。当我在文本框中输入错误的输入时,会出现RegualarExpression验证消息,并在删除输入后,然后只显示必需的验证消息。否则,不会出现必需的验证消息。
这是我的代码:
- 模型类
public class Email
{
[Required(ErrorMessage = "Email Address is required!")]
[RegularExpression("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9] {1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$", ErrorMessage = "Invalid Email Address.")]
public virtual string EmailAddress { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "First Name is Required")]
public virtual string FirstName { get; set; }
public virtual string MiddleName { get; set; }
[Required(ErrorMessage = "Last Name is required!")]
public virtual string LastName { get; set; }
public virtual string Status { get; set; }
}
- 查看
@{Html.EnableClientValidation();}
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"> </script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm("CreateEmail", "LeaderBoard", FormMethod.Post, new { enctype = "multipart/form-data", id = "create-email-dialog" }))
{
@Html.ValidationSummary(true)
{
<form>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="popTable">
<tr>
<td width="38%">
First Name:
</td>
<td width="62%">
<div class="editor-field">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
</td>
</tr>
<tr>
<td width="38%">
Middle Name:
</td>
<td width="62%">
<div class="editor-field">
@Html.EditorFor(model => model.MiddleName)
@Html.ValidationMessageFor(model => model.MiddleName)
</div>
</td>
</tr>
<tr>
<td width="38%">
Last Name:
</td>
<td width="62%">
<div class="editor-field">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
</td>
</tr>
<tr>
<td width="38%">
Email Address:
</td>
<td width="62%">
<div class="errorMessage">
@Html.EditorFor(model => model.EmailAddress)
@Html.ValidationMessageFor(model => model.EmailAddress)
</div>
</td>
</tr>
<tr>
<td width="38%">
Status:
</td>
<td width="62%">
<div class="editor-field">
@Html.DropDownList("Status", ViewData["Status"] as SelectList)
</div>
</td>
</tr>
</table>
<input type="button" id="btnCreateEmail" value="Save" name="submit" class="btn" />
<input type="button" value="Cancel" name="cancel" class="closeDialog" />
</form>
}
}
- 控制器
[HttpGet]
public ActionResult CreateEmail()
{
if (Request.IsAjaxRequest())
{
var list = new SelectList(new[]
{
new{Id = "Active", Name = "Active"},
new{Id = "Inactive", Name = "Inactive"}
},
"Id", "Name", 1);
ViewData["Status"] = list;
return PartialView("_CreateEmail");
}
return RedirectToAction("Index");
}
[HttpPost]
public ActionResult CreateEmail(Email model)
{
using (_emailRepository)
{
if (ModelState.IsValid)
{
_emailRepository.Create(model);
return Json(new { success = true });
}
return RedirectToAction("Index");
}
}
答案 0 :(得分:0)
验证文档就绪功能
中的那些字段 $( document ).ready( function ( e )
{
$( "form" ).validate().element( "#FirstName" );
$( "form" ).validate().element( "#LastName" );
});