Tuesday, February 12, 2019

C# Regex for alphabet, number and alpha-number

This blog will demonstrates how to write a regular expression to validate input value is only alphabet
or numeric or alpha-number or alpha-number with space but no special character are allowed.



There are few scenarios.

1. Only Alphabet are allowed.
^[a-zA-Z]*$

For above reg expression, valid input contains only alphabets.

Valid Scenario:  input value is only alphabet.

C# Code:
    string regExpression = @"^[a-zA-Z]*$";
    string input = "Rajeev";
    Console.WriteLine(Regex.IsMatch(input, regExpression));
    Console.ReadKey();

Console Output: True

Invalid Scenario: input value contains alphabet and numbers.

C# Code:
    string regExpression = @"^[a-zA-Z]*$";
    string input = "Rajeev123";
    Console.WriteLine(Regex.IsMatch(input, regExpression));
    Console.ReadKey();

Console Output: False


2.Only Number are allowed.
^[0-9]*$

For above reg expression, valid input contains only numbers.

Valid Scenario:  input value is only numbers.

C# Code:

    string regExpression = @"^[0-9]*$";
    string input = "9988234";
    Console.WriteLine(Regex.IsMatch(input, regExpression));
    Console.ReadKey();

Console Output: True

Invalid Scenario:  input value contains alphabet and numbers.

C# Code:
    string regExpression = @"^[0-9]*$";
    string input = "878Rajeev";
    Console.WriteLine(Regex.IsMatch(input, regExpression));
    Console.ReadKey();

Console Output: False


3.  Only alphabet – number are allowed.
       ^[a-zA-Z0-9]*$

for above reg expression, valid input contains only alphabet or number but special characters are not allowed.


Valid Scenario:  input value is only alphabet and numbers.

C# Code:
  string regExpression = @"^[a-zA-Z0-9]*$";
   string input = "Reg123";
   Console.WriteLine(Regex.IsMatch(input, regExpression));
   Console.ReadKey();

Console Output: True

Invalid Scenario:  input value contains invalid characters.

C# Code:
    string regExpression = @"^[a-zA-Z0-9]*$";
      string input = "Reg@123";
     Console.WriteLine(Regex.IsMatch(input, regExpression));
     Console.ReadKey();

Console Output: False


4.       Alphabet – number with space allowed :
               ^[a-zA-Z][a-zA-Z0-9\s]*$

for above reg expression, valid input should start with alphabet letter and after that it could be any alphabet or number or space but special characters are not allowed.

for space, ‘\s’ character is used in regular expression and there is one additional block  ^[a-zA-Z] in above expression for first letter validation, first letter should be always alphabet.

Valid Scenario:  input value contains alphabet and number with spaces.

C# Code:
string regExpression = @"^[a-zA-Z][a-zA-Z0-9\s]*$";
      string input = "rajeev Tiwari 83";
      Console.WriteLine(Regex.IsMatch(input, regExpression));
      Console.ReadKey();

Console Output:  True

Invalid Scenario:  input value contains invalid characters.

C# Code:
string regExpression = @"^[a-zA-Z][a-zA-Z0-9\s]*$";
      string input = "rajeev Tiwari@83";
      Console.WriteLine(Regex.IsMatch(input, regExpression));
      Console.ReadKey();


Thanks for visiting and please leave your comments, if you have any questions

No comments:

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...