FOLLOW

FOLLOW

SHARE

Data Validation And Formatting In ASP.NET MVC

Think of it as data baseball

5Dec

Think of it as data baseball. The bases are where customers or prospects can enter their details, such as name, address, telephone number or email address in order to set up an account, request a callback and similar. As a data quality company we obviously need to ensure that the data entered is accurate and consistently formatted in order for us to be able to use that information effectively. As there are many such touchpoints we don’t want to have to repeat the same checks time and again.

We use the ASP.NET MVC framework extensively on our website which makes the first part - ensuring that the data entered is valid - quite straightforward. On each email address and telephone number field, for example, we can add an [EmailValidation] or [TelephoneValidation] attribute to call our validation web services and reject any invalid emails or telephones. Our data model for registering a new account then looks something like:

public class NewUserModel
{
  [Required]
  [EmailValidation]
  [DataType(DataType.EmailAddress)]
  public string Email { get; set; }

  [Required]
  [DataType("FirstName")]
  public string FirstName { get; set; }

  [Required]
  [DataType("LastName")]
  public string LastName { get; set; }

  [Required]
  [TelephoneValidation]
  [DataType(DataType.PhoneNumber)]
  public string ContactNumber { get; set; }

  // ...
}

Now we’re happy that we’re getting data for our required fields, and that data is being validated, is there anything we can do to ensure its consistently formatted? We don’t want all lower-case names or some telephone numbers with a +44 and some without, we want to be able to search records by phone number without having to think about the various different ways it could have been written and we want to be able to do mail merges on names without having to standardize the case each time.

Enter a custom action filter. The ASP.NET MVC framework allows filters to be registered before and after the main action is executed, giving us a chance to intercept the entered data and standardise it before the action itself executes, allowing the action to focus on its core responsibility and delegating the formatting of the supplied data to another class.

Our [StandardizeFormatting] filter can be applied to any action. It will look through the metadata for the action parameters to find any properties it can standardize by looking at the corresponding [DataType] attributes.

Any property with an email address data type will be converted to lower case, as that’s how most people are accustomed to seeing them. Although strictly speaking it is possible to have case-sensitive mailbox names (the bit before the @ symbol), it’s so rare as to be not worth worrying about.

Any property with a telephone number data type will be standardised by running it through our telephone formatting web service. This will standardise things like the spacing and brackets in a telephone number, and will remove a country code if it matches the country value from the same model. So if you enter your address as being in the UK and enter a UK telephone number we will remove any leading +44, but if you enter a UK telephone number for a French address we would leave it in place.

There is no built-in data type for names so we have used our own; “FirstName” and “LastName”. Any data for these types are converted to proper case, but with last names having some special case handling for prefixes such as Mc, Mac and O’.

With our filter built, we just have to use it. We could add it to individual actions by decorating them with the [StandardizeFormatting] attribute, e.g.

[StandardizeFormatting]
public ActionResult Register(NewUserModel model)
{
  // ...
}

But we’ve chosen to register it globally in our application startup code as:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
  // ...

  filters.Add(new StandardizeFormattingAttribute());
}

All the actions on our website will now take advantage of the standardised formatting without any further changes. All we need to do is ensure all the model properties have the appropriate [DataType] attribute in place.

All the code from this post is on our GitHub account, or you can just grab the Data8.MvcValidation NuGet package to add it straight into your project!

Sources

Data8 Blog

Comments

comments powered byDisqus
Data sec small

Read next:

The 3 Cyber Security Governance Challenges Of The CIO

i