Validating Usernames in an Angular Frontend with Asp.Net Core Identity on the Server

Georg Dangl by Georg Dangl in Web Development Wednesday, July 05, 2017

Wednesday, July 05, 2017

Validating Usernames in an Angular Frontend with Asp.Net Core Identity on the Server
Posted in Angular Asp.Net Core

In Angular, you've got the built-in [pattern] form validation directive that makes it easy to validate an input with a regular expression. Today, I wanted to check usernames consistently across my app and I found out that the Asp.Net Core Identity backend uses just a long string which contains all allowed characters. This could look like this: "abcACB123._"

It's configured this way in your Startups ConfigureServices method:

That's not regex, so you've got two options: convert it into regex or create a custom directive that validates consistently with your backend. I've chosen option two, and want to share how quick that was:

The ValidateUsernameDirective simply checks the input string letter-by-letter for illegal chars. In template forms, you use it just like this:

Happy validating!


Share this post


comments powered by Disqus

About me

Hi, my name's George! I love coding and blogging about it. I focus on all things around .Net, Web Development and DevOps.

DanglIT

Need a consultant for BIM, GAEB or Software Development?

Contact me at [email protected], +49 (173) 56 45 689 or visit my professional page!

Dangl.Blog();
// Just 💗 Coding

Social Links