Skip to content

Instantly share code, notes, and snippets.

@yemrekeskin
Created December 16, 2014 12:17
Show Gist options
  • Select an option

  • Save yemrekeskin/12dc7301236c635c4afd to your computer and use it in GitHub Desktop.

Select an option

Save yemrekeskin/12dc7301236c635c4afd to your computer and use it in GitHub Desktop.

Revisions

  1. yemrekeskin created this gist Dec 16, 2014.
    79 changes: 79 additions & 0 deletions ExtentionSamples.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@
    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);
    }


    }




    }