C# program to demonstrate the validation of username and password

Here, we are going to demonstrate the validation of username and password in C#?
Submitted by Nidhi, on October 13, 2020

Here, we will read the username and password from the user and check the conditions for validations.

Program:

The source code to demonstrate the validation of username and password is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to demonstrate the validation of username and password.

using System;

class Demo
{
    public static int Main()
    {
        string username = "";
        string password = "";

        Console.Write("Enter Username(only 4 characters):");
        username = Console.ReadLine();

        Console.Write("Enter Password: (minimum 6 characters)");
        password = Console.ReadLine();

        if (username.Length != 4)
        {
            Console.WriteLine("Please enter correct username");
            return -1;
        }
        else if (password.Length < 6)
        {
            Console.WriteLine("Please enter correct password");
            return -1;
        }

        Console.WriteLine("Username: " + username);
        Console.WriteLine("Password: " + password);
        return 0;
    }
}

Output:

Enter Username(only 4 characters):abcd
Enter Password: (minimum 6 characters)[email protected]
Username: abcd
Password: [email protected]
Press any key to continue . . .

Explanation:

Here, we created a class Demo that contains the Main() method. The Main() method is the entry point for the program, here we created two string variables username and password. Then we read the value of the username and password. Here, we checked the length of user and password for validation, if entered username and password is correct then print them on the console screen otherwise print the error message on the console screen.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.