1

I'm trying to build a demo application for gain Asp.Net Mvc 5 development experience. In these days i'm trying to build a site settings.

I've a model named Settings. And there are properties like below.

    public int PasswordRequiredLength { get; set; }
    public bool PasswordRequireNonLetterOrDigit { get; set; }
    public bool PasswordRequireDigit { get; set; }
    public bool PasswordRequireLowercase { get; set; }
    public bool PasswordRequireUppercase { get; set; }
    public bool UseAlphaNumericCharacters {get; set; }          

In ApplicationUserManager i made thse modifications.

        // Configure validation logic for passwords
        manager.PasswordValidator = new PasswordValidator
        {
            RequiredLength = CacheHelper.Settings.PasswordRequiredLength > 0 ? CacheHelper.Settings.PasswordRequiredLength : 8,
            RequireNonLetterOrDigit = CacheHelper.Settings.PasswordRequireNonLetterOrDigit ? true : false,
            RequireDigit = CacheHelper.Settings.PasswordRequireDigit ? true : false,
            RequireLowercase = CacheHelper.Settings.PasswordRequireLowercase ? true : false,
            RequireUppercase = CacheHelper.Settings.PasswordRequireUppercase ? true : false
        };

So, password validation will be specified at run time. So far so good. The application a bit more flexible. But! What about RegisterViewModel.

There are properties like below

    [StringLength(20, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 5)]
    [RegularExpression("^([a-zA-Z0-9]{5,20})$", ErrorMessage = "The {0} must contain only alphanumeric characters")]
    [Display(ResourceType = typeof (Resources.Translations), Name = "Username")]
    public string UserName { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

How can i validate password minimum, length? Or AlphaNumeric characters if PasswordRequireNonLetterOrDigit false? Same thing on UserName. It depends UseAlphaNumericCharacters

Thank you.

6
  • you want to validate it in runtime on another data's value ? Commented Nov 29, 2015 at 9:04
  • If public bool UseAlphaNumericCharacters {get; set; } true which is comes from database. Then use [RegularExpression("^([a-zA-Z0-9]{5,20})$", ErrorMessage = "The {0} must contain only alphanumeric characters")] this validation. If false. dont use. And also maximum and minimum limitation values comes from database. Commented Nov 29, 2015 at 10:03
  • Have you tried Expressive Annotations Commented Nov 29, 2015 at 10:03
  • Or a foolproof [RequiredIf] or similar attribute Commented Nov 29, 2015 at 10:07
  • @TaşyürekGökşah Try to see RemoteAttribute Commented Nov 29, 2015 at 10:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.