Skip to the content

ASP.NET MVC Validation

Validate the data in your ASP.NET applications.


Basics

Our ASP.NET MVC package applies our advanced validation services to your models with a simple attribute.

To get started, install our NuGet package:

Install-Package Data8.MvcValidation

Add your Data8 account details to your web.config file:

<appSettings>
  <add key="Data8Username" value="your-username" />
  <add key="Data8Password" value="your-password" />
</appSettings>

You can now add our validation services to your models using the [EmailValidation] and [TelephoneValidation] attributes:

public class MyModel
{
  [EmailValidation]
  public string EmailAddress { get; set; }

  [TelephoneValidation]
  public string TelephoneNumber { get; set; }
}

Configuration

Each of the new validation attributes has a number of properties to control exactly how the validation is performed.

EmailValidationAttribute

Property Name Description Default Value
Level The level to which the email address should be validated. Choose from the following values: * `Syntax` gives the quickest response but will allow through a large number of invalid email addresses * `MX` gives a quick response and checks the domain on the right-hand side of the @ symbol is valid and configured to receive email * `Server` gives a slower response and checks the email servers to receive mail for the address are currently running * `Address` gives the slowest response but will allow through the smallest number of invalid email addresses MX

TelephoneValidationAttribute

Property Name Description Default Value
UseMobileValidation Indicates if numbers identified as being mobiles should be checked using our advanced Mobile Validation service false
UseLandlineValidation Indicates if numbers identified as being UK landlines should be checked using our advanced Landline Validation service false
DefaultCountry The country code that should be assumed when validating a telephone number if the number does not include an international prefix GB
DefaultCountryProperty The name of another property on the model class that defines the default country code that should be assumed when validating the telephone number null

If a telephone number is supplied with a full international prefix, e.g. +441513554555, the telephone number is validated according to the rules for that country. Otherwise the country is determined as follows:

  1. If the DefaultCountryProperty property is set, the country code is taken from that property
  2. If the DefaultCountryProperty is not set, or the indicated property is null or "", the country code is taken from the DefaultCountry property
  3. If the DefaultCountry property has not been set explicitly, its default value is taken from the "Data8DefaultCountry" AppSetting value from the web.config file
  4. If the "Data8DefaultCountry" AppSetting is not defined, "GB" is assumed

Standardising Formatting

Standardise the formatting of email addresses, telephone numbers and other data in your MVC models.

A consistent approach to formatting of email addresses, telephone numbers and names helps improve the usability of your application. By simply adding a [StandardizeFormatting] attribute to your action method this can be done for you automatically.

public class MyController : Controller
{
  [HttpPost]
  [StandardizeFormatting]
  public ActionResult ActionName(MyModel model)
  {
    ...
  }
}

or add it globally for all actions, e.g.:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
  filters.Add(new StandardizeFormattingAttribute());
}

Properties to be standardized within the model class are determined automatically based on the [DataType] attribute, e.g.:

public class MyModel
{
  [DataType(DataType.EmailAddress)]
  public string EmailAddress { get; set; }

  [DataType(DataType.PhoneNumber)]
  public string TelephoneNumber { get; set; }

  [DataType("Country")]
  public string CountryCode { get; set; }

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

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

  public string OtherStringProperty { get; set; }
}

In this example the values in the EmailAddress property would all be converted to lower case, the values in the TelephoneNumber property would be formatted according to the conventions for the country specified in the CountryCode property, and the FirstName and LastName properties would be proper (title) cased. The value in OtherStringProperty would be trimmed of any white space.

When formatting telephone numbers, if no property is available with a "Country" data type, the country specified in the Data8DefaultCountry application setting is used (as for telephone number validation above).

Email Validation Blogs

The number of returned mail reduced by 28% as a result of the integrated reference to the goneaway file

Business Strategy Manager, Brookson Ltd

Start a Free 30 Day Trial Today

Start a free trial today