在编辑模式下对 kendo UI 网格中的电子邮件格式应用验证

Applying validation on email format in kendo UI grid in edit mode

我正在使用 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;
    }

让我知道哪里错了。

从脚本中删除验证并在 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; }

}

在我看来,如果电子邮件在验证中有效(而不是在 checkEmail 函数上),我认为你应该 return 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;
                            }
                        }
                    },`

尝试将此正则表达式用于您的 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);