Currently Browsing Posts Tagged Sanitizing Input

Sanitizing Phone Numbers With C#

An extension method to remove all but digits from a phone number. This should be useful for sanitizing input to put into your database.

using System.Text.RegularExpressions;

namespace CustomStringMethods
{
	public static class PhoneNumber
	{
		#region Class Methods

		public static string StripAllButDigits(this string s)
		{
			return (s == null) ? string.Empty : Regex.Replace(s, @"\D", string.Empty);
		}

		#endregion
	}
}

Obviously this can be used for more than just phone numbers.