Tweaking Kentico (2): Unique fields

   —   

Today we'll learn how to make a form field unique.

In the last article we had learned how powerful validation rules in Kentico can be. Today, we'll learn more about them and I'll show you how to check if the value your visitor enters into a form is unique.

Let's say you have some form where you want to prevent the visitor from submitting multiple times. A common approach is adding some hidden field with the IP address and check if this column is unique, however there are better ways of doing this. In our case, let's create one field for the visitor's e-mail address; this field is required so that your visitor has to fill it with their e-mail address. The following macro condition will check if there is another record in the form with same e-mail address already:

General condition:

foreach (i IN SiteObjects.Forms["FormName"].Items) { if (i.email == email.Value) { return false; } } return true;

Please apply at least hotfix 9.0.19 to make this working in all scenarios.

Share this article on   LinkedIn

Jan Hermann

I am a support engineer in Kentico and I take care of any issue you have!