Esempio n. 1
0
        private void btnRegister_Click(object sender, EventArgs e)
        {
            /* VALIDATE FOR EMPTY FIELDS */

            if (String.IsNullOrEmpty(txtUsername.Text))
            {
                // Display error message if user leaves the username text field empty
                MessageBox.Show("Please enter a username.", "Consumer Survey System", MessageBoxButtons.OK, MessageBoxIcon.Error);
                // Set focus back the username text field so that the user can type their input
                txtUsername.Focus();
                // Terminate the event handler
                return;
            }
            else if (String.IsNullOrEmpty(txtPassword.Text))
            {
                // Display error message if user leaves the password text field empty
                MessageBox.Show("Please enter a password.", "Consumer Survey System", MessageBoxButtons.OK, MessageBoxIcon.Error);
                // Set focus back the password text field so that the user can type their input
                txtPassword.Focus();
                // Terminate the event handler
                return;
            }
            else if (String.IsNullOrEmpty(txtRepeatPassword.Text))
            {
                // Display error message if user leaves the repeat password text field empty
                MessageBox.Show("Please repeat your password.", "Consumer Survey System", MessageBoxButtons.OK, MessageBoxIcon.Error);
                // Set focus back the repeat password text field so that the user can type their input
                txtRepeatPassword.Focus();
                // Terminate the event handler
                return;
            }

            /* VALIDATE FOR INVALID INPUTS */

            else if (!Regex.Match(txtPassword.Text, @"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z]).{8,32}$").Success)
            {
                // Display an error message if the user enters a password that is less than 8 characters long
                MessageBox.Show("The password you entered is too weak. Your password should contain between 8 and 32 characters, at least one digit, at least one uppercase letter, at least one lowercase letter and at least one special character.", "Consumer Survey System", MessageBoxButtons.OK, MessageBoxIcon.Error);
                // Set focus back the password text field so that the user can retype their input
                txtPassword.Focus();
                // Terminate the event handler
                return;
            }

            /* CHECK IF PASSWORDS MATCH */

            else if (txtPassword.Text != txtRepeatPassword.Text)
            {
                // Display an error message if the user enters a password that is less than 8 characters long
                MessageBox.Show("Your passwords do not match.", "Consumer Survey System", MessageBoxButtons.OK, MessageBoxIcon.Error);
                // Set focus back the repeat password text field so that the user can retype their input
                txtRepeatPassword.Focus();
                // Terminate the event handler
                return;
            }
            else
            {
                /* CHECK IF USERNAME ALREADY EXISTS IN USERS TABLE */

                // Open database connection
                con.Open();
                // Select all users with a username that matches the user's input
                cmd = new SqlCommand("SELECT * FROM users WHERE username='******'", con);
                da  = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                int i = ds.Tables[0].Rows.Count;
                // Close database connection
                con.Close();
                // Check if there is a matching record in the 'users' table
                if (i > 0)
                {
                    // If the record search is positive, display an error message
                    MessageBox.Show("This username address is already taken.", "Consumer Survey System", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    // Set focus back the username text field so that the user can retype their input
                    txtUsername.Focus();
                    // Terminate the event handler
                    return;
                }
                else
                {
                    // Insert consumer information into 'users' table
                    cmd = new SqlCommand("INSERT INTO users (username, user_type, password) VALUES ('" + txtUsername.Text + "', 'consumer', '" + txtPassword.Text + "')", con);
                    // Open database connection
                    con.Open();
                    // Execute query
                    cmd.ExecuteNonQuery();
                    // Close database connection
                    con.Close();
                    // Display success message
                    MessageBox.Show("Registration Successful!", "Consumer Survey System", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    // Hide registration form
                    this.Hide();
                    // Display login form
                    var loginform = new frmLogin();
                    loginform.Show();
                }
            }
        }