Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 SecondApp1 {
- class User {
- // Data
- public string email;
- public string password;
- // Methods
- }
- public partial class Form1 : Form {
- // Data -----------------------------------
- User[] users;
- // Methods ---------------------------------
- //Constructor
- public Form1() {
- // Creates UI (Buttons, Labels, Textboxes)
- InitializeComponent();
- ClearErrorMessages();
- // Create test user
- // Local variable
- // TestUser 1
- User testUser = new User();
- testUser.email = "123.123@asd.hu";
- testUser.password = "12345";
- // TestUser 2
- User testUser2 = new User();
- testUser2.email = "321.321@asd.hu";
- testUser2.password = "12345";
- users = new User[2];
- users[0] = testUser;
- users[1] = testUser2;
- }
- // EventHandler
- private void loginBTN_Click(object sender, EventArgs e) {
- // Collecting inputs
- string email = emailTB.Text;
- string password = passwordTB.Text;
- // Clear error messages
- ClearErrorMessages();
- // Popup = IntelliSense
- // Email test
- if (email == string.Empty) {
- emailErrorLB.Text = "Email is required!";
- } else if (!email.Contains('@')) {
- emailErrorLB.Text = "Email must contain an @(at) sign!";
- } else if (email.Length < 5) {
- emailErrorLB.Text = "Email is too short!";
- } else if (!email.EndsWith(".hu")) {
- emailErrorLB.Text = "Email must end with (.hu)!";
- }
- // Password Test
- if (password == string.Empty) {
- pwErrorLB.Text = "Password is required!";
- } else if (password.Length < 5) {
- pwErrorLB.Text = "Password is too short!";
- }
- if (emailErrorLB.Text == string.Empty && pwErrorLB.Text == string.Empty) {
- // Find out the user's name
- // 1st method
- //string name = email.Substring(0, email.IndexOf('@'));
- // 2nd method
- // alma@gmail.com@asdjaskld@akljdfasj
- // 0. alma
- // 1. gmail.com
- //char[] firstInChar = firstName.ToCharArray();
- string fullName = email.Split('@')[0];
- string[] nameArray = fullName.Split('.');
- string firstName = nameArray[0];
- string lastName = nameArray[1];
- if (nameArray.Length == 2) {
- firstName = nameArray[0];
- lastName = nameArray[1];
- } else if (nameArray.Length == 1) {
- firstName = nameArray[0];
- lastName = "";
- }
- // Nake first letter capital
- if (nameArray.Length > 1) {
- firstName = firstName[0].ToString().ToUpper() + firstName.Substring(1);
- }
- if (nameArray.Length == 2) {
- lastName = lastName.First().ToString().ToUpper() + lastName.Substring(1);
- }
- // Check if the user is in the users array
- //Code snippet
- int index = 0;
- bool isFound = false;
- do {
- if (users[index].email == email && users[index].password == password) {
- isFound = true;
- }
- index++;
- } while (!isFound && index < users.Length);
- // For tag Ctrl K + Ctrl + C/U
- // User is in the DB
- if (isFound) {
- MessageBox.Show("Welcome, " + firstName + " " + lastName + "!");
- } else {
- MessageBox.Show("Wrong email or password");
- }
- }
- }
- private void ClearErrorMessages() {
- emailErrorLB.Text = "";
- pwErrorLB.Text = string.Empty;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement