Esempio n. 1
0
    /// <summary>
    /// Validate the specified text, returning the validated version.
    /// </summary>

    public string Validate(string val)
    {
        if (string.IsNullOrEmpty(val))
        {
            return("");
        }

        StringBuilder sb = new StringBuilder(val.Length);

        for (int i = 0; i < val.Length; ++i)
        {
            char c = val[i];
            if (onValidate != null)
            {
                c = onValidate(sb.ToString(), sb.Length, c);
            }
            else if (validation != Validation.None)
            {
                c = Validate(sb.ToString(), sb.Length, c);
            }
            if (c != 0)
            {
                sb.Append(c);
            }
        }

//         if (characterLimit > 0 && sb.Length > characterLimit)
//             return sb.ToString(0, characterLimit);

        //fix bytes length by senlin
        string tmpStr = sb.ToString();

        if (characterLimit > 0 && NGUITools.GetStringLength(tmpStr) > characterLimit)
        {
            tmpStr = tmpStr.Substring(0, tmpStr.Length - 1);
            while (NGUITools.GetStringLength(tmpStr) > characterLimit)
            {
                tmpStr = tmpStr.Substring(0, tmpStr.Length - 1);
            }
        }

        return(tmpStr);
    }
Esempio n. 2
0
    /// <summary>
    /// Insert the specified text string into the current input value, respecting selection and validation.
    /// </summary>

    protected virtual void Insert(string text)
    {
        string left  = GetLeftText();
        string right = GetRightText();
        int    rl    = right.Length;

        StringBuilder sb = new StringBuilder(left.Length + right.Length + text.Length);

        sb.Append(left);

        // Append the new text
        for (int i = 0, imax = text.Length; i < imax; ++i)
        {
            // If we have an input validator, validate the input first
            char c = text[i];

            if (c == '\b')
            {
                DoBackspace();
                continue;
            }

            // Can't go past the character limit
            if (characterLimit > 0 && NGUITools.GetStringLength(sb.ToString() + right) >= characterLimit)
            {
                break;
            }

            if (onValidate != null)
            {
                c = onValidate(sb.ToString(), sb.Length, c);
            }
            else if (validation != Validation.None)
            {
                c = Validate(sb.ToString(), sb.Length, c);
            }

            // Append the character if it hasn't been invalidated
            if (c != 0)
            {
                sb.Append(c);
            }
        }

        // Advance the selection
        mSelectionStart = sb.Length;
        mSelectionEnd   = mSelectionStart;

        // Append the text that follows it, ensuring that it's also validated after the inserted value
        for (int i = 0, imax = right.Length; i < imax; ++i)
        {
            char c = right[i];
            if (onValidate != null)
            {
                c = onValidate(sb.ToString(), sb.Length, c);
            }
            else if (validation != Validation.None)
            {
                c = Validate(sb.ToString(), sb.Length, c);
            }
            if (c != 0)
            {
                sb.Append(c);
            }
        }

        mValue = sb.ToString();
        UpdateLabel();
        ExecuteOnChange();
    }