コード例 #1
0
        //used to check all validation pertaining to registration and supply a user an error message if something went wrong
        //i used this central function because I also wanted to combine it with my registration database validation checks, and return a single bool
        public bool ValidationMessages(string name, string username, string password, string password2, string email)
        {
            //get access to database related functions for checking existing emails/usernames
            var rd = new RegistrationDatabase();
            //get tuple of booleans back so we can check for two booleans: Item1 is password match, Item2 is password at least 6 chars long
            var validatePassword = ValidatePassword(password, password2);

            if (!ValidateName(name))
            {
                MessageBox.Show("This is not a valid name. It must be at least three characters and no digits or symbols.");
                return(false);
            }

            if (!ValidateUsername(username))
            {
                MessageBox.Show("This is not a valid username. It must be at least three characters and no symbols.");
                return(false);
            }

            if (!rd.CheckForExistingUsername(username))
            {
                MessageBox.Show("This username already exists. Please choose something else.");
                return(false);
            }

            if (!validatePassword.Item1)
            {
                MessageBox.Show("Your passwords do not match.");
                return(false);
            }

            if (!validatePassword.Item2)
            {
                MessageBox.Show("Password must be at least 6 characters long.");
                return(false);
            }

            if (!ValidateEmail(email))
            {
                MessageBox.Show("This is not a valid email. Please check it and try again.");
                return(false);
            }

            if (!rd.CheckForExistingEmail(email))
            {
                MessageBox.Show("This email already has an account associated with it.");
                return(false);
            }

            return(true);
        }
コード例 #2
0
        private void btnAddUser_Click(object sender, EventArgs e)
        {
            bool success;

            var uv = new UserValidation();
            //function to validate all user inputs when registering and give error messages if invalid
            bool valid = uv.ValidationMessages(txtName.Text, txtUsername.Text, txtPassword.Text, txtPassword2.Text, txtEmail.Text);
            //access functions pertaining to registration
            var rd = new RegistrationDatabase();

            //if validation of all user inputs are valid
            if (valid)
            {
                if (checkboxAdmin.Checked)
                {
                    //insert admin into accounts database table
                    var admin = new Admins(txtName.Text, txtUsername.Text, txtPassword.Text, txtEmail.Text);
                    success = rd.AddAccount(admin);
                }
                else
                {
                    //insert user into accounts database table
                    var user = new Users(txtName.Text, txtUsername.Text, txtPassword.Text, txtEmail.Text);
                    success = rd.AddAccount(user);
                }

                //if added successfully then let user know
                if (success)
                {
                    MessageBox.Show("User created successfuly");
                    Close();
                }
                else
                {
                    MessageBox.Show("There was a problem while creating this user");
                }
            }
        }