Question

I want to create custom validation for duplicate User name and E-mail. Model is so:

public class RegisterModel
{
    [Required( ErrorMessage = "Username is empty" )]
    [StringLength( 100, ErrorMessage = "Minimum 5 symbol", MinimumLength = 5 )]
    [CustomValidation( typeof( RegisterModel ), "ValidateDuplicateUsername" )]
    [RegularExpression( @"^[a-zA-Z0-9]+$", ErrorMessage = "Username invalid" )]
    public string UserName { get; set; }

    [RegularExpression( @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage = "Enter valid e-mail" )]
    [Required( ErrorMessage = "E-mail is empty" )]
    [DataType( DataType.EmailAddress )]
    [CustomValidation( typeof( RegisterModel ), "ValidateDuplicateEmail" )]
    public string Email { get; set; }

    [Required( ErrorMessage = "Password is empty" )]
    [StringLength( 100, ErrorMessage = "Minimum 6 symbol", MinimumLength = 6 )]
    [DataType( DataType.Password )]
    public string Password { get; set; }

    [DataType( DataType.Password )]
    [Compare( "Password", ErrorMessage = "Password not same" )]
    public string ConfirmPassword { get; set; }

    public static ValidationResult ValidateDuplicateUsername( string username )
    {
        if ( username != null )
        {
            bool isValid = true;
            MembershipUserCollection users = Membership.GetAllUsers();
            foreach ( MembershipUser item in users )
            {
                if ( item.UserName.ToUpper().Equals( username.ToUpper() ) )
                {
                    isValid = false;
                }
            }

            if ( isValid )
            {
                return ValidationResult.Success;
            }
            else
            {
                return new ValidationResult( "Username exists" );
            }
        }
        else return new ValidationResult( "Minimum 5 symbol" );
    }

    public static ValidationResult ValidateDuplicateEmail( string email )
    {
        if ( email != null )
        {
            bool isValid = true;
            MembershipUserCollection users = Membership.GetAllUsers();
            foreach ( MembershipUser item in users )
            {
                if ( item.Email.ToUpper().Equals( email.ToUpper() ) )
                {
                    isValid = false;
                }
            }

            if ( isValid )
            {
                return ValidationResult.Success;
            }
            else
            {
                return new ValidationResult( "Username exists with this email" );
            }
        }
        else return new ValidationResult( "Enter valid e-mail" );
    }
}

All validations work, but my custom validations - ValidateDuplicateUsername and ValidateDuplicateEmail not work. What is wrong here?

Was it helpful?

Solution

You need to make your custom attributes implement IClientValidateable

Example: http://www.codeproject.com/Articles/275056/Custom-Client-Side-Validation-in-ASP-NET-MVC3

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top