2012-04-02 28 views
7

在我正在開發的頁面中,我有一個「電子郵件」字段和「ConfirmEmail」字段。而且要求有一個不區分大小寫的比較。MVC 3 - 比較屬性 - 在客戶端執行不區分大小寫的比較

我可以創建一個自定義屬性並擴展內置「比較」屬性的行爲。這適用於服務器端。

但我無法在客戶端實現它。我相信我們必須做一些額外的事情來讓不顯眼的jQuery去做一個不區分大小寫的比較。

+0

你有沒有考慮遠程驗證?否則,你可能會被困在寫一個驗證屬性,並且CompareAttribute是最醜陋的之一。 – 2012-04-02 18:15:51

+0

是的。這看起來像一個選項。謝謝。但是有沒有其他的方式?,以便我可以避免這個額外的Ajax調用。 – user979737 2012-04-03 16:01:29

回答

2

您可以使用MVC 3 ...這是一個建在溶液中的比較屬性...

[Compare("Email",ErrorMessage="your error message")] 
    public string ConfirmEmail { get; set; } 

更新: 我不好可能是我應該已經閱讀你的問題好...無論如何... 爲創造一個屬性(比較的重寫版本)後工作的不顯眼的方式......你需要做一些JavaScript的工作,不顯眼的客戶端驗證工作...這裏是一個示例博客文章unobtrusive client side validation with MVC 3 ...這和我在說的內容類似......如果你需要進一步的幫助......只需要回頭......我會很樂意幫助你...

這裏是一個更相關的職位......這還談到創建自定義的屬性... Creating Custom Validation Attribute (Server side and Client Side)

希望這有助於...

+0

第一個鏈接已經死亡 – Liam 2016-04-07 15:21:36

1

我不能完全肯定你在找什麼就比較屬性而言,但對於JavaScript,這將進行比較,並且可以根據結果從客戶端採取行動。

if (email.toUpperCase() == confirmEmail.toUpperCase()) { 
    alert("Emails are a match!");   
} else { 
    alert("Emails do not match"); 
} 
0

晚會有點晚,但我只是遇到了類似的問題。這是由jquery unobstrusive JavaScript文件中的錯誤引起的。更高版本將修復它,我只是跑

Install-Package jQuery.Validation.Unobtrusive

裏面裝V2,這對我來說工作正常。你的旅費可能會改變。

此問題已經正確地回答here

0

要執行不區分大小寫的比較,您可以創建自定義比較驗證程序。你會最終與此。

public string Courriel { get; set; } 

    [EqualToIgnoreCase("Courriel", ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "E00007")] 
    public string CourrielConfirmation { get; set;} 

這是ValidationAttribute:

/// <summary> 
/// The equal to ignore case. 
/// </summary> 
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)] 
public class EqualToIgnoreCase : ValidationAttribute, IClientValidatable 
{ 
    #region Constructors and Destructors 
public EqualToIgnoreCase(string otherProperty) 
    { 
     if (otherProperty == null) 
     { 
      throw new ArgumentNullException("otherProperty"); 
     } 

     this.OtherProperty = otherProperty; 
    } 

    #endregion 

    #region Public Properties 

    public string OtherProperty { [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] get; private set; } 

    #endregion 

    #region Public Methods and Operators 

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) 
    { 
     ModelClientValidationRule compareRule = new ModelClientValidationRule(); 
     compareRule.ErrorMessage = this.ErrorMessageString; 
     compareRule.ValidationType = "equaltoignorecase"; 
     compareRule.ValidationParameters.Add("otherpropertyname", this.OtherProperty); 
     yield return compareRule; 
    } 

    #endregion 

    #region Methods 

    protected override ValidationResult IsValid(object value, ValidationContext validationContext) 
    { 
     PropertyInfo basePropertyInfo = validationContext.ObjectType.GetProperty(this.OtherProperty); 

     IComparable valOther = (IComparable)basePropertyInfo.GetValue(validationContext.ObjectInstance, null); 

     IComparable valThis = (IComparable)value; 

     if (valOther.ToString().ToLower() == valThis.ToString().ToLower()) 
     { 
      return ValidationResult.Success; 
     } 
     else 
     { 
      return new ValidationResult("Error"); 
     } 
    } 

    #endregion 
} 

在客戶端,你將不得不添加此簡單的登記:

var isEqualToIgnoreCase = function (value, element, param) { 
    return this.optional(element) || 
    (value.toLowerCase() == $(param).val().toLowerCase()); 
}; 

$.validator.addMethod("equaltoignorecase", isEqualToIgnoreCase); 
$.validator.unobtrusive.adapters.add("equaltoignorecase", ["otherpropertyname"], function (options) { 
    options.rules["equaltoignorecase"] = "#" + options.params.otherpropertyname; 
    options.messages["equaltoignorecase"] = options.message; 
});