Send Text Messages in your application in C#

Sometimes, you want to do something that more than c# can do. In one of my projects, a deal bargain website requires to send out text messages to alert their members when pre-defined keywords match with the new deal submitted. It’s a great use of text messages alert to shopper who are looking for a particular keyword in the deal.

Continue reading “Send Text Messages in your application in C#”

Load Google Chart by Ajax using Asp.net MVC and jQuery

If you are looking for a modern chart tool in a modern browser, you must not not to know the free one that offer by Google – Google Chart, is a HTML5/SVG technology to provide cross-browser compatibility (including VML for older IE versions) and cross platform portability to iPhones, iPads and Android. No plugins are needed. Don’t get confused by the image chart offered by Google, which was another chart tool. Now the new HTML5 Google chart provides more interaction and better rendering to users .googlechartaspnetmvc Continue reading “Load Google Chart by Ajax using Asp.net MVC and jQuery”

How to check if string array contain a string

You don’t want to write a loop to detect if a string array contains a string and ends up searching the simplest way to do it without writing a loop over internet. This is what I found that’s most efficient for .net developers.

.Net 2.0

string[] strArray = { "Dell", "Acer", "HP" };
bool exist = Array.Exists(strArray , s=>s.ToLower().Contains("Asus"));

.Net 3.5

string[] strArray = { "Dell", "Acer", "HP" };
bool exist = strArray.Contains("Asus"));

How to strip/remove HTML in asp.net C#

Checking a user input contains html tag or not is not unfamiliar task to any web development project. Use the following strip html function in c#, you can take the html tag away from user inputs.

public static string StripHtml(string html, bool allowHarmlessTags)
{
      if (html == null || html == string.Empty)
           return string.Empty;

      if (allowHarmlessTags)
           return System.Text.RegularExpressions.Regex.Replace(html, "</?(?i:script|embed|object|frameset|frame|iframe|meta|link|style)(.|\n)*?>", string.Empty);

      return System.Text.RegularExpressions.Regex.Replace(html, "<[^>]*>", string.Empty);
}

Determine a string contains unicode Character in c#

If you need a function to determine different language such as Chinese/Japanese/French and so on. You may want to use the following function to determine if the input contains Unicode character.

Unicode strings use two bytes per character (or more), ANSI strings only one byte per character. ASCII only extends up to 127. 128..255 is commonly used for ANSI characters. Above that is only Unicode. Some characters from value 128 and above (including 128 itself) map to a unicode character with a value higher than 255 in codepage 1252.

public static bool IsUnicode(string input)
{
      const int MaxAnsiCode = 255;

      return input.ToCharArray().Any(c => c > MaxAnsiCode);
}

How to validate Canada Postal Code in C#

Canadian postal code contains both letters and numbers in 6 or 7 characters, such as M3A 1J6, or M3A1J6. Furthermore, not all 26 letters are being used in the letter section of Canadian postal code. See the function below

public static bool IsPostalCode(string postalCode)
{
 
      //Canadian Postal Code in the format of "M3A 1A5"
      string pattern = "^[ABCEGHJ-NPRSTVXY]{1}[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[ ]?[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[0-9]{1}$";
 
       Regex reg = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
 
       if (!(reg.IsMatch(postalCode)))
              return false;
       return true;
}

How to validate username in c# regular expression

We want to set the rules when user choosing their username. In most of cases, a pattern with letter and number is used. We also want to set the length of the username so it does not look ugly. We use the following regular expression to validate a username.

[a-zA-Z] => first character must be a letter
[a-zA-Z0-9] => it can contain letter and number
{5,11} => the length is between 6 to 12

public static bool IsUsername(string username)
{
      string pattern;
      // start with a letter, allow letter or number, length between 6 to 12.
      pattern = @"^[a-zA-Z][a-zA-Z0-9]{5,11}$";

      Regex regex = new Regex(pattern);
      return regex.IsMatch(username);
}