Introduction
In this article you are going to learn about how to take password input and show asterisks instead of the entered characters in the console and also check the empty value.
Tools
Visual Studio
Targeted Audience
People with basic knowledge of C# and Windows Forms.
Steps
First of all open vVsual Studio and create a new project.
Select console application .NET framework form the men, name your project as you want and click OK.
After creating the project open Program.cs and there we will create a function which will take a parameter of the entered text which will be the text that will be shown to user. The function will listen to the user every time he press a key. We will create a global variable “EnteredVal” which will contain the original text entered by the user. There will be a check which checks the pressed key is neither backspace nor enter key, then it will store the value in global variable (EnteredVal) and write an asterisk in the console.
In the next case if the pressed key is backspace, it is the case when user presses any wrong key and wants correction, we will remove the last alphabet from the global variable (EnteredVal) and the last asterisk from the console.
In the second last case we will check if the user enters a empty value or not, if he tries to enter an empty one we will show an error and ask him to enter again.
In the last case we consider that the user enters his desired password and wants to proceed. Here we will show a confirmation message that he entered his password.
Paste the code below, under your Main() function.
C# Code
- static void CheckPassword(string EnterText)
- {
- try
- {
- Console.Write(EnterText);
- EnteredVal = "";
- do
- {
- ConsoleKeyInfo key = Console.ReadKey(true);
-
- if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
- {
- EnteredVal += key.KeyChar;
- Console.Write("*");
- }
- else
- {
- if (key.Key == ConsoleKey.Backspace && EnteredVal.Length > 0)
- {
- EnteredVal = EnteredVal.Substring(0, (EnteredVal.Length - 1));
- Console.Write("\b \b");
- }
- else if (key.Key == ConsoleKey.Enter)
- {
- if (string.IsNullOrWhiteSpace(EnteredVal))
- {
- Console.WriteLine("");
- Console.WriteLine("Empty value not allowed.");
- CheckPassword(EnterText);
- break;
- }
- else
- {
- Console.WriteLine("");
- break;
- }
- }
- }
- } while (true);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
This function will check the entered value and show asterisk on the console instead of the characters entered and keep the original entered value in a global variable which can be used further. It will also ask the user to enter again if he enters an empty value. Now in your Main() function create an “enterText” string variable and assign any input text you want to show the user. Call your function and pass the enterText as a parameter.
All your code will look something like given below.
C# Code
- using System;
-
- namespace ProjInputPasswordConsole
- {
- class Program
- {
- public static string EnteredVal = "";
- static void Main(string[] args)
- {
- string enterText = "Please enter password: ";
- CheckPassword(enterText);
- Console.ReadKey();
- }
- static void CheckPassword(string EnterText)
- {
- try
- {
- Console.Write(EnterText);
- EnteredVal = "";
- do
- {
- ConsoleKeyInfo key = Console.ReadKey(true);
-
- if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
- {
- EnteredVal += key.KeyChar;
- Console.Write("*");
- }
- else
- {
- if (key.Key == ConsoleKey.Backspace && EnteredVal.Length > 0)
- {
- EnteredVal = EnteredVal.Substring(0, (EnteredVal.Length - 1));
- Console.Write("\b \b");
- }
- else if (key.Key == ConsoleKey.Enter)
- {
- if (string.IsNullOrWhiteSpace(EnteredVal))
- {
- Console.WriteLine("");
- Console.WriteLine("Empty value not allowed.");
- CheckPassword(EnterText);
- break;
- }
- else
- {
- Console.WriteLine("");
- break;
- }
- }
- }
- } while (true);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- }
- }
Now run your code and see the output. Try to enter a value and it will show an asterisk.
You can put the break point to see that the value is stored in the Global variable, which can be used further in saving in database etc.
You can test this in different cases like pressing backspace or entering an empty value. So that’s all.
Conclusion
This article gives an brief introduction about how to make an password input in console application and how to put different checks on it.