gpt4 book ai didi

c# - Asp.Net MVC 数据注释。如何在 2 个属性相等时获得客户端验证

转载 作者:太空宇宙 更新时间:2023-11-03 14:29:47 25 4
gpt4 key购买 nike

如何对两个属性进行客户端验证,例如经典密码确认密码场景。

我正在使用基于 EF 映射到我的数据库表的元数据类,代码如下。

我的类中注释掉的属性将使我得到服务器端验证,但不会得到客户端。

[MetadataType(typeof(MemberMD))]
public partial class Member
{

//[CustomValidation(typeof(MemberMD), "Verify", ErrorMessage = "The password and confirmation password did not match.")]
//[PropertiesMustMatch("Password", "ConfirmPassword", ErrorMessage = "The password and confirmation password did not match.")]
public class MemberMD
{

[Required(ErrorMessage = "Name is required.")]
[StringLength(50, ErrorMessage = "No more than 50 characters")]
public object Name { get; set; }

[Required(ErrorMessage = "Email is required.")]
[StringLength(50, ErrorMessage = "No more than 50 characters.")]
[RegularExpression(".+\\@.+\\..+", ErrorMessage = "Valid email required e.g. abc@xyz.com")]
public object Email { get; set; }


[Required(ErrorMessage = "Password is required.")]
[StringLength(30, ErrorMessage = "No more than 30 characters.")]
[RegularExpression("[\\S]{6,}", ErrorMessage = "Must be at least 6 characters.")]
public object Password { get; set; }

[Required]
public object ConfirmPassword { get; set; }

[Range(0, 150), Required]
public object Age { get; set; }


[Required(ErrorMessage = "Postcode is required.")]
[RegularExpression(@"^[a-zA-Z0-9 ]{1,10}$", ErrorMessage = "Postcode must be alphanumeric and no more than 10 characters in length")]
public object Postcode { get; set; }

[DisplayName("Security Question")]
[Required]
public object SecurityQuestion { get; set; }

[DisplayName("Security Answer")]
[Required]
[StringLength(50, ErrorMessage = "No more than 50 characters.")]
public object SecurityAnswer { get; set; }



public static ValidationResult Verify(MemberMD t)
{
if (t.Password == t.ConfirmPassword)
return ValidationResult.Success;
else
return new ValidationResult("");
}
}

任何帮助将不胜感激,因为我只做了 5 个月,请不要让我失望。

最佳答案

您只需将它放在您的 ConfirmPassword 属性中。

    [Compare("Password", ErrorMessage = "Passwords don't match.")]
[Required]
public object ConfirmPassword { get; set; }

这意味着它将比较您的 ConfirmPassword 属性和您的 Password 属性。

关于c# - Asp.Net MVC 数据注释。如何在 2 个属性相等时获得客户端验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2925370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com