public bool Validate(string value)
        {
            CancelEventArgs eventArgs = new CancelEventArgs();

            this.owner.CallValueChanging(eventArgs);
            if (eventArgs.Cancel)
            {
                return(false);
            }

            int hintPos = 0;
            MaskedTextResultHint hint = MaskedTextResultHint.Success;

            if (provider.Mask != "<>" && value != null)
            {
                provider.Set(value, out hintPos, out hint);

                // Positive hint results are successful
                if (hint > 0)
                {
                    oldValue = provider.ToString(true, true);
                    if (textBoxItem.Text == oldValue)
                    {
                        return(true);
                    }

                    textBoxItem.Text = oldValue;
                    this.owner.CallValueChanged(EventArgs.Empty);
                    return(true);
                }
            }

            //base.Text = unmaskedText;
            return(false);
        }
        private void UpdateText()
        {
            if (_provider.ToDisplayString().Equals(Text))
            {
                return;
            }

            bool success = _provider.Set(Text);

            SetText(success ? _provider.ToDisplayString() : Text, _provider.ToString(false, false));
        }
        public void LowerCaseMaskTest()
        {
            var maskedTextProvider = new MaskedTextProvider(TestToLowerCaseMask);

            maskedTextProvider.Set("AAaa");
            Assert.Equal("aaaa", maskedTextProvider.ToString());
        }
        /// <summary>
        /// Format the specified text using the specified mask, prompt
        /// character, and culture information and return the result
        /// values.
        /// </summary>
        /// <param name="mask">The mask to use.</param>
        /// <param name="text">The text to format.</param>
        /// <param name="promptChar">The prompt character to use for missing
        /// characters.  If a null character ('\x0') is specified, prompt
        /// characters are omitted.</param>
        /// <param name="culture">The culture information to use.  If null,
        /// the current culture is used.</param>
        /// <param name="hint">The result of formatting the text.</param>
        /// <param name="hintPosition">The position related to the result
        /// hint.</param>
        /// <returns>The formatted text string.</returns>
        public static string Format(string mask, string text, char promptChar,
                                    CultureInfo culture, out MaskedTextResultHint hint,
                                    out int hintPosition)
        {
            if (text == null)
            {
                text = String.Empty;
            }

            if (culture == null)
            {
                culture = CultureInfo.CurrentCulture;
            }

            MaskedTextProvider provider = new MaskedTextProvider(mask, culture);

            // Set the prompt character options
            if (promptChar != '\x0')
            {
                provider.PromptChar    = promptChar;
                provider.IncludePrompt = true;
            }

            // Format and return the string
            provider.Set(text, out hintPosition, out hint);

            // Positive hint results are successful
            if (hint > 0)
            {
                return(provider.ToString());
            }

            // Return the text as-is if it didn't fit the mask
            return(text);
        }
Beispiel #5
0
        public static string ToPhoneNumberFormat(this string phoneNumber)
        {
            if (!phoneNumber.IsEmpty())
            {
                phoneNumber = phoneNumber.Trim();

                MaskedTextProvider mtpPhone1 = new MaskedTextProvider(@"(00) 00000-0000");
                MaskedTextProvider mtpPhone2 = new MaskedTextProvider(@"(00) 0000-0000");
                //27996090383
                //2796090383
                if (phoneNumber.Length == 11)
                {
                    mtpPhone1.Set(phoneNumber);
                    return(mtpPhone1.ToString());
                }
                else if (phoneNumber.Length == 10)
                {
                    mtpPhone2.Set(phoneNumber);
                    return(mtpPhone2.ToString());
                }
                else
                {
                    return(phoneNumber);
                }
            }

            return(null);
        }
        public void LetterMaskTest()
        {
            var maskedTextProvider = new MaskedTextProvider(TestLetterMask);

            maskedTextProvider.Set("Abc");
            Assert.Equal("Abc", maskedTextProvider.ToString());
        }
        public static string Format(
            string mask,
            string text,
            char promptChar,
            CultureInfo culture,
            out MaskedTextResultHint hint,
            out int hintPosition)
        {
            if (text == null)
            {
                text = string.Empty;
            }
            if (culture == null)
            {
                culture = CultureInfo.CurrentCulture;
            }
            MaskedTextProvider maskedTextProvider = new MaskedTextProvider(mask, culture);

            if (promptChar != char.MinValue)
            {
                maskedTextProvider.PromptChar    = promptChar;
                maskedTextProvider.IncludePrompt = true;
            }
            maskedTextProvider.Set(text, out hintPosition, out hint);
            if (hint > MaskedTextResultHint.Unknown)
            {
                return(maskedTextProvider.ToString());
            }
            return(text);
        }
                public static string Format(string mask, string text, out MaskedTextResultHint hint, out int hintPosition, CultureInfo culture = null)
                {
                    if (text == null)
                    {
                        text = string.Empty;
                    }

                    if (culture == null)
                    {
                        culture = CultureInfo.InvariantCulture;
                    }

                    var provider = new MaskedTextProvider(mask, culture, true);

                    // Format and return the string
                    provider.Set(text, out hintPosition, out hint);

                    // Positive hint results are successful
                    if (hint > 0)
                    {
                        return(provider.ToString());
                    }

                    // Return the text as-is if it didn't fit the mask
                    return(text);
                }
        public void Clone()
        {
            var maskedTextProvider      = new MaskedTextProvider(TestSimpleDigitMask);
            var maskedTextProviderClone = maskedTextProvider.Clone();

            Assert.Equal(maskedTextProvider.ToString(), maskedTextProviderClone.ToString());
        }
        public void AlphanumericMaskTest()
        {
            var maskedTextProvider = new MaskedTextProvider(TestAlphanumericMask);

            maskedTextProvider.Set("Ab");
            Assert.Equal("Ab", maskedTextProvider.ToString());
        }
        public void UpperCaseMaskTest()
        {
            var maskedTextProvider = new MaskedTextProvider(TestToUpperCaseMask);

            maskedTextProvider.Set("aaAA");
            Assert.Equal("AAAA", maskedTextProvider.ToString());
        }
Beispiel #12
0
        private static string maskedText(string mask, string value)
        {
            MaskedTextProvider provider = new MaskedTextProvider(mask);

            provider.Set(value);
            return(provider.ToString());
        }
        public void ComplexDigitMaskTest()
        {
            var maskedTextProvider = new MaskedTextProvider(TestComplexDigitMask, new CultureInfo("en-US"));

            maskedTextProvider.Set("10000000");
            Assert.Equal("$100,000.00", maskedTextProvider.ToString());
        }
        /// <summary>
        /// Formata um CPF com a máscara 000.000.000-00
        /// </summary>
        /// <param name="strCpfCnpj">string Cpf</param>
        /// <returns></returns>
        public static string ToCPFFormat(this decimal cpf)
        {
            MaskedTextProvider mtpCpf = new MaskedTextProvider(@"000\.000\.000-00");

            mtpCpf.Set(cpf.ToString().PadLeft(11, '0'));
            return(mtpCpf.ToString());
        }
        public void DateTimeMaskTest()
        {
            var maskedTextProvider = new MaskedTextProvider(TestComplexDateTimeMask, new CultureInfo("en-US"));

            maskedTextProvider.Set("01012000101010");
            Assert.Equal("01/01/2000 10:10:10", maskedTextProvider.ToString());
        }
        /// <summary>
        /// Formata um CNPJ com a máscara 00.000.000/0000-00
        /// </summary>
        /// <param name="cnpj">string Cnpj</param>
        /// <returns></returns>
        public static string ToCNPJFormat(this decimal cnpj)
        {
            MaskedTextProvider mtpCnpj = new MaskedTextProvider(@"00\.000\.000/0000-00");

            mtpCnpj.Set(cnpj.ToString().PadLeft(14, '0'));
            return(mtpCnpj.ToString());
        }
        public void DigitMaskTest()
        {
            var maskedTextProvider = new MaskedTextProvider(TestDigitMask);

            maskedTextProvider.Set("+10");
            Assert.Equal("+10", maskedTextProvider.ToString());
        }
        public void DateTimeMaskTest()
        {
            CultureInfo cultureInfo        = CultureInfo.GetCultureInfo("en-US");
            string      expectedSeparator  = cultureInfo.DateTimeFormat.DateSeparator;
            var         maskedTextProvider = new MaskedTextProvider(TestComplexDateTimeMask, cultureInfo);

            maskedTextProvider.Set("01012000101010");
            Assert.Equal($"01{expectedSeparator}01{expectedSeparator}2000 10:10:10", maskedTextProvider.ToString());
        }
Beispiel #19
0
        /// <summary>
        /// Formata um CEP com a máscara 00.000-00
        /// </summary>
        /// <param name="cnpj">string CEP</param>
        /// <returns></returns>
        public static string ToCEPFormat(this string cep)
        {
            if (!cep.IsEmpty())
            {
                MaskedTextProvider mtpCEP = new MaskedTextProvider(@"00\.000-000");
                mtpCEP.Set(cep.PadLeft(8, '0'));
                return(mtpCEP.ToString());
            }

            return(null);
        }
Beispiel #20
0
        /// <summary>
        /// Formata um CPF com a máscara 000.000.000-00
        /// </summary>
        /// <param name="cpf">string Cpf</param>
        /// <returns></returns>
        public static string ToCPFFormat(this string cpf)
        {
            if (!cpf.IsEmpty())
            {
                cpf = cpf.Trim();

                MaskedTextProvider mtpCpf = new MaskedTextProvider(@"000\.000\.000-00");
                mtpCpf.Set(cpf.PadLeft(11, '0'));
                return(mtpCpf.ToString());
            }
            return(null);
        }
Beispiel #21
0
        /// <summary>
        /// Formata uma inscricao estadual com a máscara 000.000.000.000
        /// </summary>
        /// <param name="insc">string inscricaoEstadual</param>
        /// <returns></returns>
        public static string ToInscricaoEstadualFormat(this string insc)
        {
            if (!insc.IsEmpty())
            {
                insc = insc.Trim();

                MaskedTextProvider mtpInsc = new MaskedTextProvider(@"000\.000\.000\.000");
                mtpInsc.Set(insc.PadLeft(12, '0'));
                return(mtpInsc.ToString());
            }

            return(null);
        }
Beispiel #22
0
        /// <summary>
        /// formata o CNPJ
        /// </summary>
        /// <param name="cnpj">valor a ser formatado</param>
        /// <returns></returns>
        public static string FormatCNPJ(string cnpj)
        {
            string             ret = "";
            MaskedTextProvider mascara;

            cnpj = Functions.OnlyNumbers(cnpj, "-.,/").ToString();
            //cnpj
            //##.###.###/####-##
            mascara = new MaskedTextProvider(@"00\.000\.000/0000-00");
            mascara.Set(cnpj);
            ret = mascara.ToString();
            return(ret);
        }
Beispiel #23
0
        /// <summary>
        /// Formata um CNPJ com a máscara 00.000.000/0000-00
        /// </summary>
        /// <param name="cnpj">string Cnpj</param>
        /// <returns></returns>
        public static string ToCNPJFormat(this string cnpj)
        {
            if (!cnpj.IsEmpty())
            {
                cnpj = cnpj.Trim();

                MaskedTextProvider mtpCnpj = new MaskedTextProvider(@"00\.000\.000/0000-00");
                mtpCnpj.Set(cnpj.PadLeft(14, '0'));
                return(mtpCnpj.ToString());
            }

            return(null);
        }
Beispiel #24
0
        private void CreateProvider()
        {
            string text = _target.Text;

            if (_provider != null)
            {
                text = _provider.ToString(false, false);
            }

            _provider = new MaskedTextProvider(
                GetMask(_target),
                CultureInfo.CurrentCulture,
                true, // allow prompt as input
                GetPromptChar(_target),
                '\0',
                false); // Ascii only

            int testPosition;
            MaskedTextResultHint hint;

            _provider.Replace(text, 0, _provider.Length, out testPosition, out hint);
            SetText();
        }
Beispiel #25
0
 public static string FormatarCpfCnpj(string strCpfCnpj)
 {
     if (strCpfCnpj.Length <= 11)
     {
         MaskedTextProvider mtpCpf = new MaskedTextProvider(@"000\.000\.000-00");
         mtpCpf.Set(ZerosEsquerda(strCpfCnpj, 11));
         return(mtpCpf.ToString());
     }
     else
     {
         MaskedTextProvider mtpCnpj = new MaskedTextProvider(@"00\.000\.000/0000-00");
         mtpCnpj.Set(ZerosEsquerda(strCpfCnpj, 11));
         return(mtpCnpj.ToString());
     }
 }
Beispiel #26
0
 public static string FormatarTelefone(string strTelefone)
 {
     if (strTelefone.Length >= 11)
     {
         MaskedTextProvider mtpTelefone = new MaskedTextProvider(@"(00) 00000-0000");
         mtpTelefone.Set(ZerosEsquerda(strTelefone, 11));
         return(mtpTelefone.ToString());
     }
     else
     {
         MaskedTextProvider mtpTelefone = new MaskedTextProvider(@"(00) 0000-0000");
         mtpTelefone.Set(ZerosEsquerda(strTelefone, 10));
         return(mtpTelefone.ToString());
     }
 }
Beispiel #27
0
        public static string AplicarMascaraCNPJ(object Texto)
        {
            if (Texto != null &&
                Texto.ToString().Trim() != String.Empty)
            {
                MaskedTextProvider oMascara = null;

                oMascara = new MaskedTextProvider(@"99\.999\.999\/9999\-99");

                oMascara.Set(Texto.ToString());

                return(oMascara.ToString());
            }
            else
            {
                return(String.Empty);
            }
        }
Beispiel #28
0
        public static string Formatar(string valor, string mascara)
        {
            MaskedTextProvider mtpCnpj = new MaskedTextProvider(mascara);

            mtpCnpj.Set(valor);
            var formatted = mtpCnpj.ToString();

            if (formatted.IndexOf(" ") == -1)
            {
                return(formatted);
            }

            mascara = mascara.Replace("0", "#").Replace(@"\", "");

            StringBuilder dado = new StringBuilder();

            foreach (char c in valor)
            {
                if (Char.IsNumber(c) || c == 'x' || c == 'X')
                {
                    dado.Append(c);
                }
            }

            int indMascara = mascara.Length;
            int indCampo   = dado.Length;

            for (; indCampo > 0 && indMascara > 0;)
            {
                if (mascara[--indMascara] == '#')
                {
                    indCampo -= 1;
                }
            }

            StringBuilder saida = new StringBuilder();

            for (; indMascara < mascara.Length; indMascara++)
            {
                saida.Append((mascara[indMascara] == '#') ? dado[indCampo++] : mascara[indMascara]);
            }

            return(saida.ToString());
        }
        public void ToStringTest()
        {
            var maskedTextProvider = new MaskedTextProvider(TestSimpleDigitMask);

            maskedTextProvider.PasswordChar = '*';

            Assert.True(maskedTextProvider.Set("1234"));
            Assert.Equal("1-23-4", maskedTextProvider.ToString());
            Assert.Equal("*-**-*", maskedTextProvider.ToString(false));
            Assert.Equal("1-2", maskedTextProvider.ToString(0, 3));
            Assert.Equal("*-*", maskedTextProvider.ToString(false, 0, 3));
            Assert.Equal("*-**-*", maskedTextProvider.ToDisplayString());

            Assert.True(maskedTextProvider.Set("12"));
            Assert.Equal("1-2_-_", maskedTextProvider.ToString(true, true));
            Assert.Equal("2_-", maskedTextProvider.ToString(true, true, 2, 3));
            Assert.Equal("*_-", maskedTextProvider.ToString(false, true, true, 2, 3));
        }
        /// <summary>
        /// Checks if the editor has value or not.
        /// </summary>
        /// <returns>Return true if the editor has a value.</returns>
        private bool HasValue()
        {
            bool lResult = true;

            if (Mask != null && !Mask.Equals(string.Empty))
            {
                // Only String and Time data types can have masks in this Presentation. Check them separately.
                if (mDataType == ModelType.Time)
                {
                    // Get the Time value without Time separators in order to check if the editor control has value.
                    string lAuxStringValue = mMaskedTextBoxIT.Text.Replace(mMaskedTextBoxIT.Culture.DateTimeFormat.TimeSeparator, "");
                    lResult = !string.IsNullOrEmpty(lAuxStringValue.Trim());
                }
                else
                {
                    // String data type.
                    MaskedTextProvider maskTextProvider = new MaskedTextProvider(Mask);
                    maskTextProvider.IncludeLiterals = false;
                    if (!maskTextProvider.VerifyString(mMaskedTextBoxIT.Text))
                    {
                        // If the value assigned to the control does not satisfies the mask,
                        // check if the editor control Text has value.
                        lResult = (mMaskedTextBoxIT.Text != string.Empty);
                    }
                    else
                    {
                        // If the value assigned satisfies the mask
                        // (it is not mandatory that the mask has been completed),
                        // assign the value to the MaskTextProvider and then check if
                        // it has value.
                        maskTextProvider.Set(mMaskedTextBoxIT.Text);
                        string auxString = maskTextProvider.ToString(false, false);
                        lResult = !string.IsNullOrEmpty(auxString);
                    }
                }
            }
            else
            {
                lResult = !mMaskedTextBoxIT.Text.Equals(string.Empty);
            }
            return(lResult);
        }