Beispiel #1
0
        /// <summary>
        /// 字符串校验
        /// </summary>
        /// <param name="validat">校验类型</param>
        /// <param name="text">输入字符串</param>
        /// <param name="count">限制长度</param>
        /// <returns></returns>
        public static string Validate(CharacterValidation validat, string text, int count)
        {
            if (count > 0)
            {
                if (count > text.Length)
                {
                    text = text.Substring(0, count);
                }
            }
            switch (validat)
            {
            case CharacterValidation.Integer:
                return(ValidateInteger(text));

            case CharacterValidation.Decimal:
                return(ValidateDecimal(text));

            case CharacterValidation.Alphanumeric:
                return(ValidateAlphanumeric(text));

            case CharacterValidation.EmailAddress:
                return(ValidateEmailAddress(text));
            }
            return(text);
        }
Beispiel #2
0
        /// <summary>
        /// 字符串校验
        /// </summary>
        /// <param name="validat">校验类型</param>
        /// <param name="text">输入字符串</param>
        /// <param name="pos">字符位置</param>
        /// <param name="ch">插入字符</param>
        /// <returns></returns>
        public static char Validate(CharacterValidation validat, string text, int pos, char ch)
        {
            if (validat == CharacterValidation.None)
            {
                return(ch);
            }
            if (pos > text.Length)
            {
                pos = text.Length;
            }
            if (validat == CharacterValidation.Integer)
            {
                if (ch == '-')
                {
                    if (text == "")
                    {
                        return(ch);
                    }
                    if (text.Length > 0)
                    {
                        return((char)0);
                    }
                }
                if (ch <'0' | ch> '9')
                {
                    return((char)0);
                }
                return(ch);
            }
            else if (validat == CharacterValidation.Decimal)
            {
                if (ch >= '0' && ch <= '9')
                {
                    if (ch == '.')
                    {
                        if (text.IndexOf('.') < 0)
                        {
                            return(ch);
                        }
                    }
                    return((char)0);
                }
                return(ch);
            }
            else if (validat == CharacterValidation.Alphanumeric)
            {
                // All alphanumeric characters
                if (ch >= 'A' && ch <= 'Z')
                {
                    return(ch);
                }
                if (ch >= 'a' && ch <= 'z')
                {
                    return(ch);
                }
                if (ch >= '0' && ch <= '9')
                {
                    return(ch);
                }
            }
            else if (validat == CharacterValidation.numberAndName)
            {
                if (char.IsLetter(ch))
                {
                    // Character following a space should be in uppercase.
                    if (char.IsLower(ch) && ((pos == 0) || (text[pos - 1] == ' ')))
                    {
                        return(char.ToUpper(ch));
                    }

                    // Character not following a space or an apostrophe should be in lowercase.
                    if (char.IsUpper(ch) && (pos > 0) && (text[pos - 1] != ' ') && (text[pos - 1] != '\''))
                    {
                        return(char.ToLower(ch));
                    }

                    return(ch);
                }

                if (ch == '\'')
                {
                    // Don't allow more than one apostrophe
                    if (!text.Contains("'"))
                    {
                        // Don't allow consecutive spaces and apostrophes.
                        if (!(((pos > 0) && ((text[pos - 1] == ' ') || (text[pos - 1] == '\''))) ||
                              ((pos < text.Length) && ((text[pos] == ' ') || (text[pos] == '\'')))))
                        {
                            return(ch);
                        }
                    }
                }

                if (ch == ' ')
                {
                    // Don't allow consecutive spaces and apostrophes.
                    if (!(((pos > 0) && ((text[pos - 1] == ' ') || (text[pos - 1] == '\''))) ||
                          ((pos < text.Length) && ((text[pos] == ' ') || (text[pos] == '\'')))))
                    {
                        return(ch);
                    }
                }
                if (ch >= '0' && ch <= '9')
                {
                    return(ch);
                }
            }
            else if (validat == CharacterValidation.Name)
            {
                if (char.IsLetter(ch))
                {
                    // Character following a space should be in uppercase.
                    if (char.IsLower(ch) && ((pos == 0) || (text[pos - 1] == ' ')))
                    {
                        return(char.ToUpper(ch));
                    }

                    // Character not following a space or an apostrophe should be in lowercase.
                    if (char.IsUpper(ch) && (pos > 0) && (text[pos - 1] != ' ') && (text[pos - 1] != '\''))
                    {
                        return(char.ToLower(ch));
                    }

                    return(ch);
                }

                if (ch == '\'')
                {
                    // Don't allow more than one apostrophe
                    if (!text.Contains("'"))
                    {
                        // Don't allow consecutive spaces and apostrophes.
                        if (!(((pos > 0) && ((text[pos - 1] == ' ') || (text[pos - 1] == '\''))) ||
                              ((pos < text.Length) && ((text[pos] == ' ') || (text[pos] == '\'')))))
                        {
                            return(ch);
                        }
                    }
                }

                if (ch == ' ')
                {
                    // Don't allow consecutive spaces and apostrophes.
                    if (!(((pos > 0) && ((text[pos - 1] == ' ') || (text[pos - 1] == '\''))) ||
                          ((pos < text.Length) && ((text[pos] == ' ') || (text[pos] == '\'')))))
                    {
                        return(ch);
                    }
                }
            }
            else if (validat == CharacterValidation.EmailAddress)
            {
                if (ch >= 'A' && ch <= 'Z')
                {
                    return(ch);
                }
                if (ch >= 'a' && ch <= 'z')
                {
                    return(ch);
                }
                if (ch >= '0' && ch <= '9')
                {
                    return(ch);
                }
                if (ch == '@' && text.IndexOf('@') == -1)
                {
                    return(ch);
                }
                if (EmailCharacters.IndexOf(ch) != -1)
                {
                    return(ch);
                }
                if (ch == '.')
                {
                    char lastChar = (text.Length > 0) ? text[UnityEngine.Mathf.Clamp(pos, 0, text.Length - 1)] : ' ';
                    char nextChar = (text.Length > 0) ? text[UnityEngine.Mathf.Clamp(pos + 1, 0, text.Length - 1)] : '\n';
                    if (lastChar != '.' && nextChar != '.')
                    {
                        return(ch);
                    }
                }
            }
            return((char)0);
        }