示例#1
0
        public static bool FilterName(ref string name, out string message, int minLen = 3, int maxLen = 20)
        {
            const string AllowedCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890+-=/!#$%^&()? .,\'\"";

            if (name == null)
            {
                message = $"The name cannot be empty";
                return(false);
            }

            for (int i = 0; i < name.Length; i++)
            {
                if (!AllowedCharacters.Contains(name[i]))
                {
                    message = $"The name contains the illegal character '{name[i]}'";
                    return(false);
                }
            }

            name = name.Trim();

            for (int i = 1; i < name.Length; i++)
            {
                if (name[i] == name[i - 1] && name[i] == ' ')
                {
                    name.Remove(i, 1);
                    i--;
                }
            }

            if (name.Length < minLen)
            {
                message = $"The name needs to be at least {minLen} characters long, yours is {name.Length}.";
                return(false);
            }
            if (name.Length > maxLen)
            {
                message = $"The name needs to be maximum {maxLen} characters long, yours is {name.Length}.";
                return(false);
            }

            message = "Valid name";
            return(true);
        }
示例#2
0
        /// <summary>
        /// Overridden OnKeyPress method
        /// </summary>
        /// <param name="e"></param>
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if (AllowPaste && RaiseCustomPasteEvent && _pasteDetected && OnPaste != null)
            {
                PasteEventArgs args = new PasteEventArgs();
                args.Text = Clipboard.GetText();

                OnPaste(this, args);

                SelectedText = args.Text;
                e.Handled    = true;
            }
            else if ((AllowCut && _cutDetected) || (AllowCopy && _copyDetected))
            {
                e.Handled = false;
            }
            else if (AcceptsReturn && e.KeyChar == '\r')
            {
                e.Handled = false;
            }
            else
            {
                if (!String.IsNullOrEmpty(AllowedCharacters))
                {
                    e.Handled = !AllowedCharacters.Contains(e.KeyChar.ToString());

                    if (e.Handled && e.KeyChar == '\b' && AllowBackSpace)
                    {
                        e.Handled = !e.Handled;
                    }
                }
                else
                {
                    base.OnKeyPress(e);
                }
            }

            if (_pasteDetected)
            {
                _pasteDetected = false;
            }
        }
示例#3
0
        /// <inheritdoc />
        public Task <IdentityResult> ValidateAsync(UserManager <TUser> manager, TUser user, string password)
        {
            var result = IdentityResult.Success;

            // if AllowedCharacters is not defined in appsettings.json then the validator always returns success
            if (string.IsNullOrEmpty(AllowedCharacters))
            {
                return(Task.FromResult(result));
            }
            var isValid = password.All(x => AllowedCharacters.Contains(x));

            if (!isValid || string.IsNullOrWhiteSpace(password))
            {
                result = IdentityResult.Failed(new IdentityError {
                    Code        = ErrorDescriber,
                    Description = MessageDescriber.PasswordContainsNotAllowedChars
                });
            }
            return(Task.FromResult(result));
        }