MVC 3 - Compare attribute - perform client side insenstive comparison

On the page I'm developing, I have an Email field and a ConfirmEmail field. And the requirement is to have a case insensitive comparison.

I could create a custom attribute and extend the behavior of the Compare attribute, which is inline. This works on the server.

But I could not achieve this on the client side. I'm sure we need to do some extra things to make unobtrusive jquery case insensitive comparisons.

+7
source share
4 answers

You can use the compare attribute in MVC 3 ... which is a built-in solution ...

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

Update: my bad one, I probably should have read your question better ... anyway ... for an unobtrusive way of working after creating an attribute (overridden version of Compare) ... you need to do some javascript work for unobtrusive client-side validation to work ... here is an example blog post unobtrusive client side check with MVC 3 ... something similar to what I'm talking about ... if you need more help ... just respond ... I I will be happy to help you with this ...

Here's a more appropriate post ... which also talks about creating a custom attribute ... Creating a custom validation attribute (server-side and client-side)

Hope this helps ...

+2
source

I'm not quite sure what you are looking for, as the Compare attribute, but for JavaScript it will do a comparison and you can take action from the client based on the results.

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

A bit late, but I just ran into a similar problem. This is caused by a jquery unobstrusive javascript file error. A later version will fix this, I just ran

Install-Package jQuery.Validation.Unobtrusive

which installed v2, which works fine for me. Your mileage may vary.

This question is correctly answered here .

0
source

To perform case insensitive comparisons, you can create your own comparison validator. You are done with this.

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

This is the ValidationAttribute attribute:

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

On the client side, you will have to add this simple registration:

 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; }); 
0
source

All Articles