我正在使用kendo UI
网格。使用它编辑内联以添加新记录。
我有三个字段FirstName,LastName和Email。
到目前为止,validations
的一切工作正常。我需要在电子邮件中应用validation
来检查其格式。
电子邮件验证已正确应用且工作正常。但是我遇到了FirstName和LastName字段的问题。这些字段在具有值时仍会提供所需的消息。
以下是我正在使用的整个代码:
schema: {
data: 'Items',
total: 'TotalRows',
model: {
id: 'ID',
fields: {
ID: { type: 'integer' },
FName: { type: 'string', validation: { required: true, validationMessage: 'First name is required.' } },
LName: { type: 'integer', validation: { required: true, validationMessage: 'Last name is required.' } },
Email: {
type: 'string',
validation: {
required: { message: "EMail ID Required." },
validateEmailFormat: function (input) {
if (input.attr("data-bind") == "value:Email") {
input.attr("data-validateEmailFormat-msg", "Email format invalid.");
return checkEmail(input.val());
}
}
}
},
UserLevelsID: { type: 'integer', defaultValue: userLevelID },
RoleName: { type: 'string' }
}
}
}
function checkEmail(val) {
var re = /^(([^<>()[\]\\.,;:\s@@\"]+(\.[^<>()[\]\\.,;:\s@@\"]+)*)|(\".+\"))@@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (val == "") {
return false;
} else if (val.search(re) == -1) {
return false;
}
return true;
}
让我知道我哪里错了。
答案 0 :(得分:2)
从脚本中删除验证并在Kendo推荐的模型级别上添加验证
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using DataAnnotationsExtensions;
public class RegisterModel
{
[System.Web.Mvc.HiddenInput(DisplayValue = false)]
public int UserId { get; set; }
[Required(ErrorMessage = "You forgot to enter a username.")]
[StringLength(50, ErrorMessage = "The {0} must be at least {2} characters long and 50 characters short", MinimumLength = 3)]
[RegularExpression(@"([a-zA-Z\d]+[\w\d]*|)[a-zA-Z]+[\w\d.]*", ErrorMessage = "Invalid User name. No space allowed between charachters.")]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required(ErrorMessage = "First name is required.")]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Last name is required.")]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Required(ErrorMessage = "Email is required.")]
[Display(Name = "Email")]
[DataType(DataType.EmailAddress, ErrorMessage = "Invalid email address")]
[EmailAddress(ErrorMessage = "Invalid email address")]
[RegularExpression(@"^[._A-Za-z0-9-\\+]+(\\.[._A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$", ErrorMessage = "Invalid email address")]
public string Email { get; set; }
[Required(ErrorMessage = "Password is required. minimum of 6 characters must include letters, numbers, lower case and a upper case.")]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
[MembershipPassword()]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Required]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
[Required]
public string RoleId { get; set; }
[Required]
[Min(1, ErrorMessage = "Site is Required.")]
public int SiteId { get; set; }
public bool IsEnabled { get; set; }
}
答案 1 :(得分:0)
在我看来,如果电子邮件在验证中有效(不在checkEmail函数上),我认为你应该返回true,否则它将继续检查你的电子邮件。因此,在电子邮件地址有效后,它将检查您的名字和姓氏。
以下是示例:
Email: {
type: 'string',
validation: {
required: { message: "EMail ID Required." },
validateEmailFormat: function (input) {
if (input.attr("data-bind") == "value:Email") {
input.attr("data-validateEmailFormat-msg", "Email format invalid.");
return checkEmail(input.val());
}
return true;
}
}
},`
答案 2 :(得分:0)
尝试将此正则表达式用于checkEmail函数。
function validateEmail(email) {
var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}
(?:\.[a-z]{2})?)$/i;
return re.test(email);