2011-06-07 367 views
2

jQuery的客戶端驗證我創建了一個自定義驗證屬性:自定義屬性

public sealed class DateAttribute : DataTypeAttribute 
    { 
     /// <summary> 
     /// Initializes a new instance of the <see cref="EmailAddressAttribute"/> class. 
     /// </summary> 
     public DateAttribute() : base(DataType.Date) 
     { 
     } 

     /// <summary> 
     /// Checks that the value of the data field is valid. 
     /// </summary> 
     /// <param name="value">The data field value to validate.</param> 
     /// <returns> 
     /// true always. 
     /// </returns> 
     public override bool IsValid(object value) 
     { 
      DateTime inputDate = Convert.ToDateTime(value, CultureInfo.CurrentCulture); 

      if (inputDate.Date >= DateTime.Now.Date.AddMonths(-2) && inputDate.Date <= DateTime.Now.Date.AddMonths(2)) 
       return true; 

      return false; 
     } 
} 

上面的代碼需要回發到服務器,我怎麼能得到這個使用jQuery在客戶端的工作嗎?

感謝, -Naren

回答

2

你不能讓這段代碼工作的客戶端自動的。您必須在JavaScript中編寫類似的驗證代碼,並配置jquery.validation以應用此驗證方法。

+0

能否請你幫我,我怎麼能寫在JavaScript代碼similarr,什麼是將要採取的步驟。 – 2011-06-07 12:52:27

+0

這聽起來不容易。您必須將日期解析爲JavaScript Date對象,並且解析將隨着不同的文化而變化。如果我是你,我會想到其他一些日期處理方式。例如,http://jqueryui.com/demos/datepicker/ – Zruty 2011-06-07 14:15:56