Exemplo n.º 1
0
        /// <summary>
        /// Test user registration info.
        /// </summary>
        /// <param name="value">Validation parameters.</param>
        /// <returns>Results array for each parameter.</returns>
        public static ValidationField[] ValidateMemberRegistration(
            string firstName,
            string lastName,
            string email,
            string userName,
            string password,
            string birthday,
            string gender,
            bool?terms,
            bool?news,
            UserFieldName requiredFields = 0
            )
        {
            var results = new List <ValidationField>();
            var values  = new List <ValidationField>()
            {
                new ValidationField(UserFieldName.FirstName, firstName ?? ""),
                new ValidationField(UserFieldName.LastName, lastName ?? ""),
                new ValidationField(UserFieldName.Email, email ?? ""),
                new ValidationField(UserFieldName.UserName, userName ?? ""),
                new ValidationField(UserFieldName.Password, password ?? ""),
                new ValidationField(UserFieldName.Birthday, birthday ?? ""),
                new ValidationField(UserFieldName.Gender, gender ?? ""),
                new ValidationField(UserFieldName.Terms, terms),
                new ValidationField(UserFieldName.News, news),
            };
            var allValues = (UserFieldName[])Enum.GetValues(typeof(UserFieldName));

            if (requiredFields == 0)
            {
                requiredFields = (UserFieldName)allValues.Select(x => (int)x).Sum();
            }
            foreach (var item in allValues)
            {
                var description = ClassLibrary.ClassTools.EnumTools.GetDescription(item);
                if (requiredFields.HasFlag(item))
                {
                    var  field  = values.FirstOrDefault(x => x.Name == item);
                    bool isEmpy = field.Value is string
                                  ?string.IsNullOrEmpty((string)field.Value)
                                      : field.Value == null;

                    if (isEmpy)
                    {
                        results.Add(new ValidationField(item, field.Value, "<b>" + description + "</b> is required."));
                    }
                    else if (field.Value is string)
                    {
                        var value = (string)field.Value;
                        if (DisallowedNames.Contains(value))
                        {
                            results.Add(new ValidationField(item, value, "This <b>" + description + "</b> is not allowed."));
                        }
                        if ((item == UserFieldName.FirstName || item == UserFieldName.LastName) && Regex.Match(value, "[a-zA-Z]{1,20}").Value != value)
                        {
                            results.Add(new ValidationField(item, value, "<b>" + description + "</b> must be between 1-20 characters<br/> and contain only letters."));
                        }
                        if (item == UserFieldName.UserName)
                        {
                            if (Regex.Match(value, "[a-zA-Z0-9_]{3,20}").Value != value)
                            {
                                results.Add(new ValidationField(item, value, "<b>" + description + "</b> must be between 3-20 characters<br />and contain only letters or numbers."));
                            }
                            else if (User.GetUser(value) != null)
                            {
                                results.Add(new ValidationField(item, value, "" + description + " is already taken."));
                            }
                        }
                        if (item == UserFieldName.Email)
                        {
                            if (!EmailRegex.Match(email).Success)
                            {
                                results.Add(new ValidationField(item, value, "" + description + " is not valid."));
                            }
                            else if (User.GetUserByEmail(email) != null)
                            {
                                results.Add(new ValidationField(item, value, "" + description + " is already taken."));
                            }
                        }
                        if (item == UserFieldName.Password)
                        {
                            if (value.Length < 6)
                            {
                                results.Add(new ValidationField(item, value, "" + description + " must be at least 6 characters."));
                            }
                            if (password == firstName)
                            {
                                results.Add(new ValidationField(item, value, "" + description + " can't be same as First Name."));
                            }
                            if (password == lastName)
                            {
                                results.Add(new ValidationField(item, value, "" + description + " can't be same as Last Name."));
                            }
                            if (password == userName)
                            {
                                results.Add(new ValidationField(item, value, "" + description + " can't be same as User Name."));
                            }
                            if (password == email)
                            {
                                results.Add(new ValidationField(item, value, "" + description + " can't be same as Email."));
                            }
                            if (!string.IsNullOrEmpty(firstName) && value.Contains(firstName))
                            {
                                results.Add(new ValidationField(item, value, "" + description + " can't contain First Name."));
                            }
                            if (!string.IsNullOrEmpty(lastName) && value.Contains(lastName))
                            {
                                results.Add(new ValidationField(item, value, "" + description + " can't contain Last Name."));
                            }
                            if (!string.IsNullOrEmpty(userName) && value.Contains(userName))
                            {
                                results.Add(new ValidationField(item, value, "" + description + " can't contain User Name."));
                            }
                        }
                        if (item == UserFieldName.Birthday)
                        {
                            DateTime bd;
                            if (!DateTime.TryParse(birthday, out bd))
                            {
                                results.Add(new ValidationField(item, value, description + " is not valid."));
                            }
                        }
                        if (item == UserFieldName.Gender)
                        {
                            if (gender != "M" && gender != "F")
                            {
                                results.Add(new ValidationField(item, value, description + " is not valid."));
                            }
                        }
                    }
                    else
                    {
                        var bValue = (bool?)field.Value;
                        if (item == UserFieldName.Terms)
                        {
                            if (!bValue.Value)
                            {
                                results.Add(new ValidationField(item, bValue, "You must accept <b>" + description + "</b>."));
                            }
                        }
                    }
                }
            }
            return(results.ToArray());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Test user registration info.
        /// </summary>
        /// <param name="value">Validation parameters.</param>
        /// <returns>Results array for each parameter.</returns>
        public static ValidationField[] ValidateMemberRegistration(
            string firstName,
            string lastName,
            string email,
            string username,
            string password,
            string birthday,
            string gender,
            bool?terms,
            bool?news
            )
        {
            string f = string.Empty; // First Name
            string l = string.Empty; // Last Name
            string e = string.Empty; // Email
            string u = string.Empty; // Username
            string p = string.Empty; // Password
            string b = string.Empty; // Birthday
            string g = string.Empty; // Gender
            string t = string.Empty; // Terms
            string n = string.Empty; // Promotions

            // Required fields.
            if (string.IsNullOrEmpty(firstName))
            {
                f = "<b>First Name</b> is required.";
            }
            if (string.IsNullOrEmpty(lastName))
            {
                l = "<b>Last Name</b> is required.";
            }
            if (string.IsNullOrEmpty(email))
            {
                e = "<b>Your Email</b> is required.";
            }
            if (string.IsNullOrEmpty(username))
            {
                u = "<b>Username</b> is required.";
            }
            if (string.IsNullOrEmpty(password))
            {
                p = "<b>Password</b> is required.";
            }
            // First Name
            if (string.IsNullOrEmpty(f) && firstName.Length > 0)
            {
                //if (Engine.DataBase.Data.Resource.IsMatch(firstName, AppContext.Current.Resources.DisallowedNames))
                //{
                //    f = "This first name is not allowed.";
                //}
            }
            // Last Name
            if (string.IsNullOrEmpty(l) && lastName.Length > 0)
            {
                //if (Engine.DataBase.Data.Resource.IsMatch(lastName, AppContext.Current.Resources.DisallowedNames))
                //{
                //    l = "This last name is not allowed.";
                //}
            }
            // Username.
            if (string.IsNullOrEmpty(u) && Regex.Match(username, "[a-zA-Z0-9_]{3,20}").Value != username)
            {
                u = "<b>Username</b> must be between 3-20 characters<br />and contain only letters or numbers.";
            }
            if (string.IsNullOrEmpty(u) && User.GetUser(username) != null)
            {
                u = "<b>Username</b> is already taken.";
            }
            //if (string.IsNullOrEmpty(u) && Engine.Forums.Transforms.IsMatch(
            //        username, Engine.Forums.Transforms.DisallowedNames))
            //{
            //    u = "<b>Username</b> is not available.";
            //}
            // Email
            if (string.IsNullOrEmpty(e) && !EmailRegex.Match(email).Success)
            {
                e = "<b>Your Email</b> address is not valid.";
            }

            if (string.IsNullOrEmpty(e) && User.GetUserByEmail(email) != null)
            {
                e = "<b>Your Email</b> address is used already.";
            }
            // Password
            if (string.IsNullOrEmpty(p) && password.Length < 6)
            {
                p = "<b>Password</b> must be at least 6 characters.";
            }
            // Pasword: Is same as...
            //if (string.IsNullOrEmpty(p) && password == firstName)
            //{
            //    p = "<b>Password</b> can't be same as <b>First Name</b>.";
            //}
            //if (string.IsNullOrEmpty(p) && password == lastName)
            //{
            //    p = "<b>Password</b> can't be same as <b>Last Name</b>.";
            //}
            //if (string.IsNullOrEmpty(p) && password == username)
            //{
            //    p = "<b>Password</b> can't be same as <b>Username</b>.";
            //}
            if (string.IsNullOrEmpty(p) && password == email)
            {
                p = "<b>Password</b> can't be same as <b>Your Email</b>.";
            }
            // Password: Contains...
            //if (string.IsNullOrEmpty(p) && password.Contains(firstName))
            //{
            //    p = "<b>Password</b> can't contain <b>First Name</b> inside.";
            //}
            //if (string.IsNullOrEmpty(p) && password.Contains(lastName))
            //{
            //    p = "<b>Password</b> can't contain <b>Last Name</b> inside.";
            //}
            //if (string.IsNullOrEmpty(p) && password.Contains(username))
            //{
            //    p = "<b>Password</b> can't contain <b>Username</b> inside.";
            //}
            // Birthday
            DateTime bd;

            if (string.IsNullOrEmpty(birthday) || !DateTime.TryParse(birthday, out bd))
            {
                b = "Indicate your <b>Birthday</b> to register.";
            }
            // Gender
            if (gender != "M" && gender != "F")
            {
                g = "Indicate your <b>Gender</b> to register.";
            }
            // Terms
            if (terms.HasValue && terms.Value == false)
            {
                t = "You must accept <b>Terms of use</b>.";
            }
            List <ValidationField> results = new List <ValidationField>();

            results.Add(new ValidationField()
            {
                Name = UserFieldName.FirstName, Value = firstName, Message = f
            });
            results.Add(new ValidationField()
            {
                Name = UserFieldName.LastName, Value = lastName, Message = l
            });
            results.Add(new ValidationField()
            {
                Name = UserFieldName.Email, Value = email, Message = e
            });
            results.Add(new ValidationField()
            {
                Name = UserFieldName.UserName, Value = username, Message = u
            });
            results.Add(new ValidationField()
            {
                Name = UserFieldName.Password, Value = password, Message = p
            });
            results.Add(new ValidationField()
            {
                Name = UserFieldName.Birthday, Value = birthday, Message = b
            });
            results.Add(new ValidationField()
            {
                Name = UserFieldName.Gender, Value = gender, Message = g
            });
            results.Add(new ValidationField()
            {
                Name = UserFieldName.Terms, Value = terms, Message = t
            });
            results.Add(new ValidationField()
            {
                Name = UserFieldName.News, Value = news, Message = n
            });
            return(results.ToArray());
        }