Quick Tip: Changing How Emails Are Validated in Kentico

   —   

Kentico has a lot of great capabilities out of the box for validating data submitted by users. For many companies, these default settings and configurations are just fine for running their sites and capturing information from their users. Occasionally, a company will encounter a specific challenge that requires them to modify how a particular type of data is validated. In this blog, I’ll show you how you can leverage a setting key to change how Kentico validates email addresses within a site.

Data validation is a tricky part of any application. Every company has different requirements and rules, so defining a validation process that suits everyone’s needs is pretty much impossible. That being said, Kentico does a great job of doing some base-line checking of inputs that fit most people’s needs. There are, of course, exceptions to this plan, and some applications require very specific guidelines to ensure their data is in the correct format.

Get ready. I’m about to start talking about Regular Expressions.

Let me start this by stating, no DECLARING, that I am in no way a RegEx expert. I certainly have used them over the years, but have tried my best to stay out of the spiraling rabbit hole that is writing expressions and implementing them everywhere in my site.

That being said, they certainly have their place in modern applications. Ensuring that the entered data matches the expected format is crucial to proper coding and functionality—regular expressions are the cornerstone of security within any application. And luckily, Kentico has a lot of great security capabilities already built-in to ensure things like dates, numbers, and email addresses are formatted correctly. But what if you have a specific format for your emails or domain?

The CMSEmailValidationRegex Web.Config Key

This web.config application key can be added to any site and allows you to specify a new regular expression for your email addresses. If this key is found, Kentico will automatically use it to validate the email address input types anywhere in the system.

Let’s say you wanted to accept emails only from a specific domain. Here’s what the regular express for that may look like:

^[a-zA-Z]+@kentico\.com$

This simple expression ensures that any email address entered must be on the kentico.com domain. If not, the expressions will return false. You probably will have much more complex requirements for your emails, but the concept is the same.

To test the validation, I created a basic form within my site with an email field.

Basic Form With Email

I then entered an email address that did not match the kentico.com domain and submitted it.

Submit With Failed Email

I then updated the email to one that did use the kentico.com domain and confirmed it validated.

Submit With Successful Email

Moving Forward

There are lots of these built-in keys and settings you can leverage for your custom applications. By customizing email validation and other aspects of the system, you can fine tune your Kentico installation to match your needs, without having to add custom code. I recommend you check the full list of web.config application keys to see what other capabilities there are. Good luck!

You can find information on all of the available web.config application keys here.

Share this article on   LinkedIn

Bryan Soltis

Hello. I am a Technical Evangelist here at Kentico and will be helping the technical community by providing guidance and best practices for all areas of the product. I might also do some karaoke. We'll see how the night goes...