0
[Required]
[ValidatePasswordLength]

[DataType(DataType.Password)] 
[Display(Name = "Password")]
[Minimumthreenumbers]
public string Password { get; set; }

public class MinimumthreenumbersAttribute : ValidationAttribute 
{

    private const string _defaultErrorMessage = "There should be minimum three letters in the string";

    private string Otherpassword;

    public MinimumthreenumbersAttribute() : base(_defaultErrorMessage)
    {


    }

    public override bool IsValid(object value)
    {
        string i = value.ToString();
        string jobId = i;
        int digitsCount = 0;
        foreach (char c in jobId)
        {
            if (Char.IsDigit(c))
                digitsCount++;
        }

        if (digitsCount > 3)
        {

            return true;
        }


        else
        {

            return false;
        }


    }
}

The above is the custom attribute implementation class .The above code actually has to validate the password to check for minimum 3 numbers.If the user entered password has less than 3 digits it should throw an error.This is the requirement. But is not working as expected. Any ideas on how to make the above code working? I have tried for some time but still it is not working.

1
  • How is it not working? You have to tell us what it's doing and what you expect it to do. We're not mind readers. Commented Jan 17, 2014 at 22:02

2 Answers 2

2

Not sure that will be enough to make your code "work", but at least this will simplify your code.

public class MinimumthreenumbersAttribute : ValidationAttribute 
{
   public MinimumthreenumbersAttribute() : base("There should be minimum three letters in the string")
   {
   }

   public override bool IsValid(object value)
   {
       return value != null && 
              value.ToString()
                   .Where(Char.IsDigit)
                   .Count() >=3
   }
}
Sign up to request clarification or add additional context in comments.

10 Comments

Raphal it is still not working as expected . The password should have atleaset three didgits . I was wondering is there any mandatory need to implement the IClientValidatable interface ?
@user2465036 be more precise that "not working as expected". What do you really expect which is not happening ?
The code actually has to validate the password to check for minimum 3 numbers.If the user entered a password which is less than 3 digits it should throw an error.This is the requirement
Currently even though i enter 2 numbers ,it is accepting and not throwing an error. The requirement is it is not supposed to allow a password in which there are less than 3 numbers
Server side ..public class MinimumthreenumbersAttribute : ValidationAttribute { public MinimumthreenumbersAttribute() : base("There should be minimum three letters in the string") { } public override bool IsValid(object value) { return value != null && value.ToString() .Where(Char.IsDigit) .Count() >=3 } }
|
0

You could just use a RegularExpressionAttribute to ensure there are three digits:

[RegularExpression(@".*\d.*\d.*\d.*")]
public string Password { get; set; }

1 Comment

Rubie. Thanks for the solution the password is validatin ,but i am interested in that custom validation . I want to learn about how to overide the base class methods and whats going on the custom validation

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.