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);
}

How to validate email in .Net c#/vb.net

Validation becomes a basic task in any form of application development especially the email validation which is required by any web application that allows user registration.

The following code sample is utilizing the regular expression pattern to validate the email address. If you have any other way of validating emails, please comment below.

C# sample

public static bool IsEmail(string strIn)
{
      if (string.IsNullOrEmpty(strIn))
           return false;
 
      return Regex.IsMatch(strIn,
             @"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?             @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$");
}

 
VB.Net Sample

Public Shared Function IsEmail(strIn As String) As Boolean
    If String.IsNullOrEmpty(strIn) Then
        Return False
    End If
 
    Return Regex.IsMatch(strIn, "^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))" & "(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$")
End Function