Skip to content

Instantly share code, notes, and snippets.

@yemrekeskin
Created December 16, 2014 12:17
Show Gist options
  • Save yemrekeskin/12dc7301236c635c4afd to your computer and use it in GitHub Desktop.
Save yemrekeskin/12dc7301236c635c4afd to your computer and use it in GitHub Desktop.
ExtentionSamples
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace ExtentionSamples
{
class Program
{
static void Main(string[] args)
{
//(xxx)xxx-xxxx: (123)456-7890
//(xxx) xxx-xxxx: (123) 456-7890
//xxx-xxx-xxxx: 123-456-7890
//xxxxxxxxxx: 1234567890
string c1 = "(123)456-7890";
string c2 = "(123) 456-7890";
string c3 = "123-456-7890";
string c4 = "1234567890";
if (c1.IsValidPhone()) Console.WriteLine("Correct :D"); else Console.WriteLine("Wrong :O");
if (c2.IsValidPhone()) Console.WriteLine("Correct :D"); else Console.WriteLine("Wrong :O");
if (c3.IsValidPhone()) Console.WriteLine("Correct :D"); else Console.WriteLine("Wrong :O");
if (c4.IsValidPhone()) Console.WriteLine("Correct :D"); else Console.WriteLine("Wrong :O");
Console.WriteLine();
//xxxxx-xxxx: 01234-5678
//xxxxx: 01234
string z1 = "01234-5678";
string z2 = "01234";
if (z1.IsValidZip()) Console.WriteLine("Correct :D"); else Console.WriteLine("Wrong :O");
if (z2.IsValidZip()) Console.WriteLine("Correct :D"); else Console.WriteLine("Wrong :O");
Console.WriteLine();
string dummy = "()h{e??l#'l>>o<<";
dummy = dummy.RemoveNonAlphaNumericCharacters();
Console.WriteLine(dummy);
Console.WriteLine();
Console.ReadKey();
}
}
public static class ValidExtention
{
public static bool IsValidPhone(this string candidate)
{
return Regex.IsMatch(candidate, @"^\(?\d{3}\)?[\s\-]?\d{3}\-?\d{4}$");
}
public static bool IsValidZip(this string candidate)
{
return Regex.IsMatch(candidate, @"^\d{5}(\-\d{4})?$");
}
public static string RemoveNonAlphaNumericCharacters(this string input)
{
return Regex.Replace(input, @"[^\w\.@-]", string.Empty);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment