2015-02-10 55 views
0

我正在使用kendo UI網格。使用它來內聯編輯以添加新記錄。 我有三個字段FirstName,LastName和Email。 直到現在一切工作正常validations。我需要在電子郵件上申請validation以檢查其格式。在編輯模式下在kendo UI網格中對電子郵件格式應用驗證

電子郵件驗證已正確應用,並且工作正常。但是我正在面對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 = /^(([^<>()[\]\\.,;:\[email protected]@\"]+(\.[^<>()[\]\\.,;:\[email protected]@\"]+)*)|(\".+\"))@@((\[[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

LName:{type:'integer',驗證:{required:true,validationMessage:'姓氏是必需的。' }},是一種整數....你期待Lname中的整數值 – Shaz 2015-02-10 11:16:29

+0

@Shaz它沒有解決問題... – 2015-02-10 15:18:31

回答

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; 
          } 
         } 
        },` 
+0

謝謝,它的工作原理。 – 2015-02-26 09:34:04

1

從腳本中刪除了驗證,並添加通過劍道

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; } 

} 
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); 
相關問題