Its been a while since I have done this and I read about the changes in the docs but this isn't working on the client. Any help would be appreciated.
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)] public class NonAlphaUppercaseAttribute : ValidationAttribute, IClientModelValidator { protected override ValidationResult IsValid(object value, ValidationContext validationContext) { string password = value.ToString(); bool nonAlpha = false; bool uppercase = false; foreach(var character in password) { if(!char.IsLetterOrDigit(character)) { nonAlpha = true; break; } } foreach (var character in password) { if (char.IsUpper(character)) { uppercase = true; break; } } if(!nonAlpha || !uppercase) { return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName)); } return null; }// IsValid() public void AddValidation(ClientModelValidationContext context) { MergeAttribute(context.Attributes, "data-val", "true"); var errorMessage = FormatErrorMessage(context.ModelMetadata.GetDisplayName()); MergeAttribute(context.Attributes, "data-val-nonalphauppercase", errorMessage); }// AddValidation() private bool MergeAttribute(IDictionary<string, string> attributes, string key, string value) { if (attributes.ContainsKey(key)) { return false; } attributes.Add(key, value); return true; } }// NonAlphaUppercaseAttribute class
(function (win, doc, undefined) { var nonalpha = false; var uppercase = false; var isValid = function (value) { var pattern = /\W|_/; nonalpha = pattern.test(value); var pattern2 = /[A-Z]/; uppercase = pattern2.test(value); var result = (nonalpha === true && uppercase === true); return result; }; jQuery.validator.addMethod('NonAlphaUppercase', isValid); jQuery.validator.unobtrusive.adapters.add('nonalphauppercase', {}, function (options) { options.rules['nonalphauppercase'] = true; options.messages['nonalphauppercase'] = options.message; }); })(window, document);
[Required] [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)] [NonAlphaUppercase(ErrorMessage = "{0} must have an uppercase and non-alpha numeric character")] [DataType(DataType.Password)] [Display(Name = "password")] public string Password { get; set; }
Thanks in advance!