How to check if enter key pressed in javascript

In a user specific designed application or website, we want application start taking action without user click extra button. Especially in the scenario of search function. You want user be able to start searching when enter key is pressed on the textbox.

function checkEnter(e){
var keyCode = (e.keyCode ? e.keyCode : e.which);
if(keyCode == 13){
//perform action
alert(‘enter key pressed’)
}
}

[html]
<input type="text" onkeypress="checkEnter(event);" />
[/html]
[javascript]
<script>
function checkEnter(e){
var keyCode = (e.keyCode ? e.keyCode : e.which);
if(keyCode == 13){
//perform action
alert(‘enter key pressed’)
}
}
</script>
[/javascript]]

Click here to see the jquery version

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, &quot;&lt;/?(?i:script|embed|object|frameset|frame|iframe|meta|link|style)(.|\n)*?&gt;&quot;, string.Empty);

      return System.Text.RegularExpressions.Regex.Replace(html, &quot;&lt;[^&gt;]*&gt;&quot;, 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 =&gt; c &gt; 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 = @&quot;^[a-zA-Z][a-zA-Z0-9]{5,11}$&quot;;

      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

Country Drop Down Html Helper for Asp.net MVC

Almost every website or web application need to display countries in the drop down list. If that’s true to your project, the following country drop down html helper for Asp.net MVC may save some time for you.

First, we need all the countries information so that we can put into the select list. Thanks to Mads, we can use the XML countries list. or you can download the countries XML List.

Second, A html helper is created for populating the country data into actual HTML format. Simply put the xml file in the App_Data or any folder that works the best for your application.

Continue reading “Country Drop Down Html Helper for Asp.net MVC”