Reputation: 6317
I use ASP.NET Membership to manage users on my site. But I need to create the user accounts manually, without the help of the CreateUserWizard. I have set textboxes for the required fields, and now I am setting up validators.
The question is: How do I make a validator that would see if the password the user enters in the textbox is valid?
I know that there is a specific format the password must have, but that can be changed, and I would like for the validator to work even if that changes. So a regular expression validator won't work (i think)
Upvotes: 2
Views: 4682
Reputation: 38842
There doesn't seem to be a simple way. However, you can use the Membership.CreateUser
that takes a MembershipCreateStatus
param.
If the password is valid, the created user object will be null and the MembershipCreateStatus will be set to InvalidPassword
(or any other creation status).
Example:
MembershipCreateStatus membershipCreateStatus;
MembershipUser newUser = Membership.CreateUser(userName, password, email, passwordQuestion, passwordAnswer, true, out membershipCreateStatus);
// Check if the user was created succesfully
if (newUser == null)
{
// membershipCreateStatus contains the information why the creation was not successful
if (membershipCreateStatus == MembershipCreateStatus.InvalidPassword)
{
// The password doesn't match the requirements
}
}
Upvotes: 3
Reputation: 26956
What's your objection to using (a variant) of the CreateUserWizard? If it's to do with needing additional fields, or the layout there are ways around that:
But even the default CreateUserWizard doesn't do client side password validation, I guess for this very reason :(
Upvotes: 0
Reputation: 23766
You can dynamically set the regex of a regular expression validator.
private void Page_Load(object sender, EventArgs e)
{
passwordValidator.ValidationExpression = someConfiguration.GetPasswordValidationExpression();
}
Upvotes: 0