0151 355 4555

Telephone & Mobile Number Validation in ASP.NET

03/02/2010 by Mark Carrington

Our Data Capture & Validation pack includes ASP.NET controls to enable you to add telephone number validation and email address validation along with postcode lookup functionality to your web forms.

We have recently updated the telephone number validator to include mobile number validation as well. If you've already got the validators installed, download the new version now from our developer site.

This new telephone number validator has two properties that control how the entered telephone number is validated - UseTelephoneValidation and UseMobileValidation.

  1. If UseTelephoneValidation is true, the telephone number is first validated using our telephone validation web service. This checks if the number is in a valid range of numbers, is of an appropriate length and other checks. If this fails, the telephone number is marked as invalid.
  2. If UseMobileValidation is true, the number appears to be a mobile number and it has not already been marked as invalid by the telephone validation service, the number is then validated again using our mobile validation web service. This enables mobile numbers to be validated precisely, while landline numbers are still validated to the exchange level in step 1.

If either of these steps fails, the number is marked as invalid (the validator's IsValid property is set to false).

In addition, the precise values returned by each validator service are exposed via the TelephoneValidationStatus and MobileValidationStatus properties respectively. When the validator detects an error, you can use the values provided by these properties to display a more relevant error message or perform other logic as necessary, e.g.

protected void SubmitButton_Click(object sender, EventArgs e)
{
  if (!telephoneValidator.IsValid)
  {
    if (telephoneValidator.TelephoneValidationStatus == false)
    {
      telephoneValidator.ErrorMessage = "Please check your telephone number";
    }
    else
    {
      switch (telephoneValidator.MobileValidationStatus)
      {
        case MobileValidationStatus.Invalid :
          telephoneValidator.ErrorMessage = "Please check your mobile number";
          break;

        case MobileValidationStatus.Unavailable :
        case MobileValidationStatus.TimedOut :
          telephoneValidator.ErrorMessage = "Your mobile appears to be unavailable. Please enter a different number";
          break;

        case MobileValidationStatus.Error :
          telephoneValidator.ErrorMessage = "We cannot validate your mobile number at present. Please enter a different number";
          break;
      }
    }
  }

  if (!Page.IsValid)
    return;

  // TODO: Regular form submission code.
}

Because our validator controls follow the standard ASP.NET validator pattern, you do not have to write any special code to work with them - just drop them onto your page and set up their properties as normal. They even support client side validation, but please be aware that the client side validation and server side validation for the same form will cause your account to be debited twice. If you do not require client side validation, please set the EnableClientScript property to false for these validators.

< Back to Blog

Add comment