C# Code Snippet - Validate Integer

C# Code Snippet - Validate Integer

C# Code Snippet - Validate Integer

(C-Sharp) C# code snippet to validate given integer. IsInteger function validate integer both positive and negative. Function return TRUE if valid integer found, if not function will return FALSE.

Bookmark:

C# Code Snippet - Validate Integer

This .Net C# code snippet validate integer values. This function uses regular expression to match string pattern to validate integer values.

public bool IsInteger(string strNumber)
{
    System.Text.RegularExpressions.Regex _RegexNotIntPattern 
        = new System.Text.RegularExpressions.Regex("[^0-9-]");
    System.Text.RegularExpressions.Regex _RegexIntPattern 
        = new System.Text.RegularExpressions.Regex("^-[0-9]+$|^[0-9]+$");
    return !_RegexNotIntPattern.IsMatch(strNumber) && _RegexIntPattern.IsMatch(strNumber);
}


C# Keywords Used:

  • Regex
  • IsMatch

Code Snippet Information:

  • Applies To: .Net, C#, Integer Validation, Regular Expression
  • Programming Language : C#

External Resources:

Leave a comment