This document contains code for a login class that validates username and password inputs. The login class properties store the username and password. Methods validate the string and integer inputs, clear text inputs, and check if the inputs match the stored values to determine if the user is eligible to log in. The code is used in a login form where the user inputs are checked by the login class validation methods when the user clicks the login button.
1. using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace Login
{
class Login
{
//decalre properties
public string Username { get; set; }
public string Userpassword { get; set; }
//intialise
public Login(string user, string pass)
{
this.Username = user;
this.Userpassword = pass;
}
//validate string
private bool StringValidator(string input)
{
string pattern = "[^a-zA-Z]";
if(Regex.IsMatch(input,pattern))
{
return true;
}
else
{
return false;
}
}
//validate integer
private bool IntegerValidator(string input)
{
string pattern = "[^0-9]";
if(Regex.IsMatch(input,pattern))
{
return true;
}
else
{
return false;
}
}
//clear user inputs
private void ClearTexts(string user, string pass)
{
user = String.Empty;
pass = String.Empty;
}
//method to check if eligible to be logged in
internal bool IsLoggedIn(string user, string pass)
{
//check user name empty
if (string.IsNullOrEmpty(user))
{
2. MessageBox.Show("Enter the user name!");
return false;
}
//check user name is valid type
else if (StringValidator(user) == true)
{
MessageBox.Show("Enter only text here");
ClearTexts(user,pass);
return false;
}
//check user name is correct
else
{
if (Username != user)
{
MessageBox.Show("User name is incorrect!");
ClearTexts(user,pass);
return false;
}
//check password is empty
else
{
if (string.IsNullOrEmpty(pass))
{
MessageBox.Show("Enter the passowrd!");
return false;
}
//check password is valid
else if (IntegerValidator(pass) == true)
{
MessageBox.Show("Enter only integer here");
return false;
}
//check password is correct
else if(Userpassword!=pass)
{
MessageBox.Show("Password is incorrect");
return false;
}
else
{
return true;
}
}
}
}
}
}
CompleteCsharp
3. using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Login
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
//Enter code here for your version of username and userpassword
Login login = new Login("admin", "1234");
private void button1_Click(object sender, EventArgs e)
{
//define local variables from the user inputs
string user = nametxtbox.Text;
string pass = pwdtxtbox.Text;
//check if eligible to be logged in
if(login.IsLoggedIn(user,pass))
{
MessageBox.Show("You are logged in successfully");
}
else
{
//show default login error message
MessageBox.Show("Login Error!");
}
}
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventAr
gs e)
{
//enter your code for forget password case
MessageBox.Show("Under development");
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventAr
gs e)
{
//Enter your code for registration form of your choice
MessageBox.Show("Under development");
}
}
}