Last updated
August 3, 2023

How To Validate Phone Numbers In ASP.NET

Brian Wu

Table of Contents:

Get your free
Phone Validation
API key now
4.8 from 1,863 votes
See why the best developers build on Abstract
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required

Phone numbers are often used in software applications for sending messages, two-factor authentication, and other features. Also, they are often used for marketing campaigns. Therefore, when entering it is very important to check whether the phone number is correct and active.

In this article, we will show several different methods of validating phone numbers using the .net framework.

There are alternative methods for validating phone numbers that do not require writing code. One of them is using Abstract's Phone Validation API, with which you can quickly and easily validate any phone number.

Let’s send your first free
Phone Validation
See why the best developers build on Abstract
Get your free api

Methods to Validate Mobile Number With 10 Digits In ASP.NET

There is a list of easy methods for validating mobile numbers in ASP.NET. In this article, we will discuss the various methods and provide practical code examples.

Improve your contact rate and clean your lists with Abstract's industry-leading phone number verification API.

Phone Number Validation in ASP.NET Core

In net core, validation rules are defined in the model. Phone number validation uses the DataAnnotations namespace, which has two validation attributes: DataType and Phone.

You should use the DataType in your model class if your MobilePhone field is of a simple type.

public string MobilePhone { get; set; }

The DataType enumeration provides a list of standard data types supported by the class. The DataTypeAttribute class uses the DataType.PhoneNumber enumeration to get the specific data type to test.

As an alternative to the [DataType], you can use the [Phone] attribute. In this case, the class, not the enum is used for checking.

public string PhoneNumber { get; set; }

The Phone class derives from the DataTypeAttribute class and can be overridden and extended to provide additional validation behavior.

Both of these approaches are interchangeable and will work the same in most cases. However, validation using the Phone class is more complicated internally.

The listed validation methods have some disadvantages:

  1. No phone number length check.
  2. There is no verification that the phone number is valid in a particular country.
  3. A regular expression is used for data validation. By default, they accept the characters "-.()", as well as an extension marked as "ext.", "ext" or "x", which does not always correspond to the desired valid phone number format.

If you want to change the default valid number format, you can use the Phone class. In addition to the [Phone], add the [RegularExpression] attribute.

The following example uses the [RegularExpression] to check if the mobile number matches the standard 10-digit format, for example, 123-456-7890, (123) 456-7890, 123 456 7890, 123.456.7890.

public string PhoneNumber { get; set; }

Phone Number Validation in ASP.NET MVC

Model validation in mvc works just like it does in ASP.NET Core. You can use the [DataType] and [Phone] and code examples above to validate phone number formats in ASP.NET MVC and get the same result as for ASP.NET Core.

Phone Number Validation in ASP.NET WebForms

To validate mobile numbers in an old ASP.NET WebForms project, use a regex expression in the validation control, as shown in the following example.

This code creates a text box for entering a phone number, as well as a validator for it. The validator has a ValidationExpression property that contains a regular expression to check if the user entered value matches. In this example, the validator checks the entered number against the standard 10-digit format.

If the entered value does not match the pattern, then an error message is displayed, which is stored in the ErrorMessage property.

Adding Custom Phone Number Validation with Regular Expressions

A valid phone number can have different formats. They may contain country and city codes, have extensions, or include additional characters. Therefore, their validation is a rather difficult task. Regular expressions allow you to effectively deal with it. To use them in C#, you need to add the following code:

using System;
using System.Text.RegularExpressions;

Then you need to define a regular expression that will be used to validate data entry. Depending on the format that is allowed in your application, the regular expression may change.

The following regular expression matches most standard phone number formats such as

 0123456789, 012-345-6789, and (012) -345-6789:

^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$.

If you want to validate the country code, use the following regular expression:


If your application allows you to enter a valid phone number with an extension, you need to choose a different regular expression:


Once you've decided which regular expression to use, all that's left is to write the function that validates entered value. Use the IsMatch function of the Regex class to do this, as shown in the following source code:

public static bool IsPhoneValid(string phoneNumber)
string regex = @"^([\+]?33[-]?|[0])?[1-9][0-9]{8}$";
if (phoneNumber != null)
return Regex.IsMatch(number, regex);
else return false;

Alternative Methods for Validating Phone Numbers with ASP.NET

You don't have to write complex regular expressions to check whether the input number is valid. You can do this without writing a single line of code from scratch. You can use Abstract's Phone Validation API for this. This is a simple, lightweight, and fast API that allows you to validate phone numbers effortlessly, thereby increasing your productivity.

Run the following request to validate the mobile number using Abstract's Phone Validation API.

The request has two parameters:

  • phone is the phone number that must be validated.
  • api_key is the unique API key that you get after registration.

If the value is valid, you will receive the following response:

"phone": "14274392542",
"valid": true,
"international_format": "+14274392542",
"country_name": "United States of America",
"country_code": "US",
"registered_location": "San Francisco, CA",
"carrier":"Verizon USA",
"line_type": "Mobile",

The "valid" parameter indicates whether the specified value is valid or not. In addition, you get other useful data such as international_format, country_name, registered_location, and carrier.

Try Abstract's phone validation solution, use its latest features for any of your projects, and make sure your users' data is clean and accurate. 

Further Reading on Phone Number Validation

Phone Number Validation FAQs

How can I verify a phone number?

To verify a phone number, you can use automated tools such as Abstract's Phone Validation API. It allows you to easily and quickly check whether the specified phone number is valid and also provides additional information about it.  

How do I use Regularexpressionvalidator?

First, you need to create a TextBox control and a RegularExpressionValidator control. In the ControlToValidate property of the RegularExpressionValidator control, write the id of the TextBox that needs to be validated.

After that, use the ValidationExpression property of the RegularExpressionValidator control to set the regular expression according to which the data from the TextBox control will be validated. To set up the error message that should be displayed, use the ErrorMessage property of the RegularExpressionValidator control. The text color of the error message is set using the ForeColor property.

What is an alternate mobile number?

An alternate mobile number is an additional number that you can use to set up a separate line for business calls or for specific groups of people. You can use an alternate number on the same device as your main one, on a separate device, or it can be the phone number of another person who can contact you and give you information.

A mobile number that is not your primary number, but which you can use to get information intended for you, is an alternate mobile number.

Brian Wu
Get your free
Phone Validation
key now
This is some text inside of a div block.
get started for free

Related Articles

Get your free
key now
Phone Validation
4.8 from 1,863 votes
See why the best developers build on Abstract
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required