ASP.NET Core

ASP.NET Core Password Complexity Validation using a Regular Expression in a View Model

Let’s walk through how to use the RegularExpression attribute in a ASP.NET Core View Model to validate a complex password requirement during registration, in which users must use 3 of 4 different types of characters.

I’m using Auth0 and have configured passwords in the systems to have the following requirements. I want to ensure my front end is enforcing these requirements.

Regular expression requirements

In my ASP.NET Core project, which has jQuery validation, I was able to use the [RegularExpression] attribute in my view model. I could then add the regular expression and error message.

There is no simple, elegant regex to solve the above. What I have is long because it effectively checks for each of the possible scenarios.

Here’s the code and a screenshot showing it in action.

Password validation example
Example of the password validation in action

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s