示例#1
0
        public ActionResult CreationSubmittion(AccountCreation m)
        {
            //check for blank entries
            if (m.Email == null || m.Password == null || m.confirmPassword == null || m.Fname == null || m.Lname == null || m.Phone_Num == null)
            {
                ViewBag.EmptyFields = "Must not leave any blank. Try again.";
                var universities = GetAllUniversities();
                m.Universities = GetSelectListItems(universities);
                return(View("AccountCreation", m));
            }

            //Check lengths
            if (m.Phone_Num.Length > 14)
            {
                ViewBag.InvalidPhone = "Phone number too long. Try again.";
                var universities = GetAllUniversities();
                m.Universities = GetSelectListItems(universities);
                return(View("AccountCreation", m));
            }

            if (m.Email.Length > 100)
            {
                ViewBag.InvalidEmail = "Email too long. Try again.";
                var universities = GetAllUniversities();
                m.Universities = GetSelectListItems(universities);
                return(View("AccountCreation", m));
            }

            if (m.Fname.Length > 100)
            {
                ViewBag.InvalidFname = "First name too long. Try again.";
                var universities = GetAllUniversities();
                m.Universities = GetSelectListItems(universities);
                return(View("AccountCreation", m));
            }

            if (m.Lname.Length > 100)
            {
                ViewBag.InvalidLname = "Last name too long. Try again.";
                var universities = GetAllUniversities();
                m.Universities = GetSelectListItems(universities);
                return(View("AccountCreation", m));
            }

            if (m.Password.Length > 100)
            {
                ViewBag.InvalidPassword = "******";
                var universities = GetAllUniversities();
                m.Universities = GetSelectListItems(universities);
                return(View("AccountCreation", m));
            }

            //check for matching passwords
            if (!m.Password.Equals(m.confirmPassword))
            {
                ViewBag.InvalidPassword = "******";
                var universities = GetAllUniversities();
                m.Universities = GetSelectListItems(universities);
                return(View("AccountCreation", m));
            }

            //Check for invalid email
            try
            {
                MailAddress mail = new MailAddress(m.Email);
            }
            catch (FormatException)
            {
                ViewBag.InvalidEmail = "This is not a valid email address. Try again.";
                var universities = GetAllUniversities();
                m.Universities = GetSelectListItems(universities);
                return(View("AccountCreation", m));
            }

            //Check for invalid phone_num - TODO need to fix to make areacode manditory
            Regex rg = new Regex(@"^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$");

            if (!rg.IsMatch(m.Phone_Num))
            {
                ViewBag.InvalidPhone = "This is not a valid phone number. Try again.";
                var universities = GetAllUniversities();
                m.Universities = GetSelectListItems(universities);
                return(View("AccountCreation", m));
            }

            grabFromDB DB = new grabFromDB();


            //must check to see if user already exists
            if (DB.getUserListByEmail(m.Email).Count != 0)
            {
                ViewBag.InvalidEmail = "Email already used. Try a different one.";
                var universities = GetAllUniversities();
                m.Universities = GetSelectListItems(universities);
                return(View("AccountCreation", m));
            }

            if (DB.getUserListByPhone(m.Phone_Num).Count != 0)
            {
                ViewBag.InvalidPhone = "This phone number is already linked to an account. Please enter different one.";
                var universities = GetAllUniversities();
                m.Universities = GetSelectListItems(universities);
                return(View("AccountCreation", m));
            }


            DB.insertUser(m.Phone_Num, m.Email, m.University, m.Fname, m.Lname, m.Password);

            ViewBag.SuccessfullyCreated = "Your account was successfully created. Thanks for joining! Please sign in to continue.";

            LoginModel model = new LoginModel();

            return(View("LogIn", model));
        }