Products
Services
Contact Us

Scripts: C# :: Form Validation :: Library Article #7

Developer's Section

Validating Phone Numbers using regular expressions in C#
By: Erobo Team Member

Hire a Developer for Related Work / Installation | $55 hr
Rating:  | Rate It:   
Average Votes: (2148)
Favorites:

In ths tutorial we explain how to validate an input field that requires phone numbers to be entered.

The function IsPhone takes as input an string that contains the phone number digits. Then, it creates a regular expression which tries to match common phone number patterns such as ### ### ####, ###-###-####, and (###) ###-###.
See the function below:

 Code Snippet 1


//Include Regular Expressions at the top of your script
using System.Text.RegularExpressions;

private bool IsPhone(String strPhone)
{
  Regex objPhonePattern=new Regex(@"^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$");
  return objPhonePattern.IsMatch(strPhone); 
}



See other Scripts in Form Validation

Submit Your Scripts:

If you would like to have your Javascripts published in this section please fill out
the form below:
*Your Name or Username:
Home Town:
*Email:
*Description and Code:
*Enter Code shown
to the right:

[ Refresh Image ]