在我正在开发的页面中,我有一个“电子邮件”字段和“ConfirmEmail”字段。而且要求有一个不区分大小写的比较。MVC 3 - 比较属性 - 在客户端执行不区分大小写的比较
我可以创建一个自定义属性并扩展内置“比较”属性的行为。这适用于服务器端。
但我无法在客户端实现它。我相信我们必须做一些额外的事情来让不显眼的jQuery去做一个不区分大小写的比较。
在我正在开发的页面中,我有一个“电子邮件”字段和“ConfirmEmail”字段。而且要求有一个不区分大小写的比较。MVC 3 - 比较属性 - 在客户端执行不区分大小写的比较
我可以创建一个自定义属性并扩展内置“比较”属性的行为。这适用于服务器端。
但我无法在客户端实现它。我相信我们必须做一些额外的事情来让不显眼的jQuery去做一个不区分大小写的比较。
您可以使用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)
希望这有助于...
第一个链接已经死亡 – Liam 2016-04-07 15:21:36
我不能完全肯定你在找什么就比较属性而言,但对于JavaScript,这将进行比较,并且可以根据结果从客户端采取行动。
if (email.toUpperCase() == confirmEmail.toUpperCase()) {
alert("Emails are a match!");
} else {
alert("Emails do not match");
}
晚会有点晚,但我只是遇到了类似的问题。这是由jquery unobstrusive JavaScript文件中的错误引起的。更高版本将修复它,我只是跑
Install-Package jQuery.Validation.Unobtrusive
里面装V2,这对我来说工作正常。你的旅费可能会改变。
此问题已经正确地回答here。
要执行不区分大小写的比较,您可以创建自定义比较验证程序。你会最终与此。
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;
});
你有没有考虑远程验证?否则,你可能会被困在写一个验证属性,并且CompareAttribute是最丑陋的之一。 – 2012-04-02 18:15:51
是的。这看起来像一个选项。谢谢。但是有没有其他的方式?,以便我可以避免这个额外的Ajax调用。 – user979737 2012-04-03 16:01:29