GetFirstMaskPosition() public static method

public static GetFirstMaskPosition ( string text ) : int
text string
return int
Ejemplo n.º 1
0
        protected override void OnPreRender(System.EventArgs e)
        {
            base.OnPreRender(e);
            if (this.EnableClientScript)
            {
                // register Script Resource at current page
                ScriptManager.RegisterClientScriptResource(this, typeof(MaskedEditValidator), "AjaxControlToolkit.MaskedEdit.MaskedEditValidator.js");

                MaskedEditExtender MaskExt = (MaskedEditExtender)FindControl(ControlExtender);
                TextBox            Target  = (TextBox)MaskExt.FindControl(ControlToValidate);

                int FirstMaskPos     = -1;
                int LastMaskPosition = -1;
                if (MaskExt.ClearMaskOnLostFocus)
                {
                    FirstMaskPos     = 0;
                    LastMaskPosition = MaskedEditCommon.GetValidMask(MaskExt.Mask).Length + 1;
                }
                else
                {
                    FirstMaskPos     = MaskedEditCommon.GetFirstMaskPosition(MaskExt.Mask);
                    LastMaskPosition = MaskedEditCommon.GetLastMaskPosition(MaskExt.Mask) + 1;
                }
                ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "IsMaskedEdit", true.ToString().ToLower(CultureInfo.InvariantCulture), true);
                ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "ValidEmpty", this.IsValidEmpty.ToString().ToLower(CultureInfo.InvariantCulture), true);
                ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "MaximumValue", this.MaximumValue, true);
                ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "MinimumValue", this.MinimumValue, true);
                ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "InitialValue", this.InitialValue, true);
                ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "ValidationExpression", this.ValidationExpression, true);
                ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "ClientValidationFunction", this.ClientValidationFunction, true);
                ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "TargetValidator", Target.ClientID, true);
                ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "EmptyValueMessage", this.EmptyValueMessage, true);
                ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "EmptyValueText", this.EmptyValueBlurredText, true);
                ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "MaximumValueMessage", this.MaximumValueMessage, true);
                ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "MaximumValueText", this.MaximumValueBlurredMessage, true);
                ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "MinimumValueMessage", this.MinimumValueMessage, true);
                ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "MinimumValueText", this.MinimumValueBlurredText, true);
                ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "InvalidValueMessage", this.InvalidValueMessage, true);
                ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "InvalidValueText", this.InvalidValueBlurredMessage, true);
                ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "InvalidValueCssClass", MaskExt.OnInvalidCssClass, true);
                ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "CssBlurNegative", MaskExt.OnBlurCssNegative, true);
                ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "CssFocus", MaskExt.OnFocusCssClass, true);
                ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "CssFocusNegative", MaskExt.OnFocusCssNegative, true);
                ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "TooltipMessage", this.TooltipMessage, true);
                ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "FirstMaskPosition", FirstMaskPos.ToString(CultureInfo.InvariantCulture), true);

                if (!String.IsNullOrEmpty(MaskExt.CultureName))
                {
                    ControlCulture = System.Globalization.CultureInfo.GetCultureInfo(MaskExt.CultureName);
                }
                switch (MaskExt.MaskType)
                {
                case MaskedEditType.None:
                {
                    ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "evaluationfunction", "MaskedEditValidatorNone", true);
                    ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "LastMaskPosition", LastMaskPosition.ToString(CultureInfo.InvariantCulture), true);
                    break;
                }

                case MaskedEditType.Number:
                {
                    string AttibCu = ControlCulture.NumberFormat.CurrencySymbol;
                    string AttibDc = ControlCulture.NumberFormat.CurrencyDecimalSeparator;
                    string AttibTh = ControlCulture.NumberFormat.CurrencyGroupSeparator;
                    if (MaskExt.DisplayMoney != MaskedEditShowSymbol.None)
                    {
                        LastMaskPosition += MaskExt.CultureCurrencySymbolPlaceholder.Length + 1;
                    }
                    if (MaskExt.AcceptNegative != MaskedEditShowSymbol.None)
                    {
                        if (MaskExt.DisplayMoney != MaskedEditShowSymbol.None)
                        {
                            LastMaskPosition++;
                        }
                        else
                        {
                            LastMaskPosition += 2;
                        }
                    }
                    ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "Money", AttibCu, true);
                    ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "Decimal", AttibDc, true);
                    ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "Thousands", AttibTh, true);
                    ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "evaluationfunction", "MaskedEditValidatorNumber", true);
                    ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "LastMaskPosition", LastMaskPosition.ToString(CultureInfo.InvariantCulture), true);
                    break;
                }

                case MaskedEditType.DateTime:
                {
                    //date
                    string   AttibSep = ControlCulture.DateTimeFormat.DateSeparator;
                    char     sep      = System.Char.Parse(ControlCulture.DateTimeFormat.DateSeparator);
                    string[] arrDate  = ControlCulture.DateTimeFormat.ShortDatePattern.Split(sep);
                    string   AttibFmt = arrDate[0].Substring(0, 1).ToUpper(ControlCulture);
                    AttibFmt += arrDate[1].Substring(0, 1).ToUpper(ControlCulture);
                    AttibFmt += arrDate[2].Substring(0, 1).ToUpper(ControlCulture);

                    AttibFmt = (MaskExt.UserDateFormat == MaskedEditUserDateFormat.None ? AttibFmt : MaskExt.UserDateFormat.ToString());

                    ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "DateSeparator", AttibSep, true);
                    ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "DateFormat", AttibFmt, true);
                    ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "Century", MaskExt.Century.ToString(CultureInfo.InvariantCulture), true);
                    //time
                    AttibSep = ControlCulture.DateTimeFormat.TimeSeparator;
                    string AttibSyb = "";
                    if (String.IsNullOrEmpty(ControlCulture.DateTimeFormat.AMDesignator + ControlCulture.DateTimeFormat.PMDesignator))
                    {
                        AttibSyb = "";
                    }
                    else
                    {
                        AttibSyb = ControlCulture.DateTimeFormat.AMDesignator + ";" + ControlCulture.DateTimeFormat.PMDesignator;
                    }

                    AttibSyb = (MaskExt.UserTimeFormat == MaskedEditUserTimeFormat.None ? AttibSyb : "");

                    if (MaskExt.AcceptAMPM)
                    {
                        if (!String.IsNullOrEmpty(AttibSyb))
                        {
                            sep = System.Char.Parse(AttibSep);
                            string[] arrSyb = AttibSyb.Split(sep);
                            LastMaskPosition += arrSyb[0].Length + 1;
                        }
                    }
                    ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "TimeSeparator", AttibSep, true);
                    ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "AmPmSymbol", AttibSyb, true);
                    ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "evaluationfunction", "MaskedEditValidatorDateTime", true);
                    ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "LastMaskPosition", LastMaskPosition.ToString(CultureInfo.InvariantCulture), true);
                    break;
                }

                case MaskedEditType.Date:
                {
                    string   AttibSep = ControlCulture.DateTimeFormat.DateSeparator;
                    char     sep      = System.Char.Parse(ControlCulture.DateTimeFormat.DateSeparator);
                    string[] arrDate  = ControlCulture.DateTimeFormat.ShortDatePattern.Split(sep);
                    string   AttibFmt = arrDate[0].Substring(0, 1).ToUpper(ControlCulture);
                    AttibFmt += arrDate[1].Substring(0, 1).ToUpper(ControlCulture);
                    AttibFmt += arrDate[2].Substring(0, 1).ToUpper(ControlCulture);

                    AttibFmt = (MaskExt.UserDateFormat == MaskedEditUserDateFormat.None ? AttibFmt : MaskExt.UserDateFormat.ToString());

                    ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "DateSeparator", AttibSep, true);
                    ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "DateFormat", AttibFmt, true);
                    ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "Century", MaskExt.Century.ToString(CultureInfo.InvariantCulture), true);
                    ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "evaluationfunction", "MaskedEditValidatorDate", true);
                    ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "LastMaskPosition", LastMaskPosition.ToString(CultureInfo.InvariantCulture), true);
                    break;
                }

                case MaskedEditType.Time:
                {
                    string AttibSep = ControlCulture.DateTimeFormat.TimeSeparator;
                    string AttibSyb = "";
                    if (String.IsNullOrEmpty(ControlCulture.DateTimeFormat.AMDesignator + ControlCulture.DateTimeFormat.PMDesignator))
                    {
                        AttibSyb = "";
                    }
                    else
                    {
                        AttibSyb = ControlCulture.DateTimeFormat.AMDesignator + ";" + ControlCulture.DateTimeFormat.PMDesignator;
                    }

                    AttibSyb = (MaskExt.UserTimeFormat == MaskedEditUserTimeFormat.None ? AttibSyb : "");

                    if (MaskExt.AcceptAMPM)
                    {
                        if (!String.IsNullOrEmpty(AttibSyb))
                        {
                            char     sep    = System.Char.Parse(AttibSep);
                            string[] arrSyb = AttibSyb.Split(sep);
                            LastMaskPosition += arrSyb[0].Length + 1;
                        }
                    }
                    ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "TimeSeparator", AttibSep, true);
                    ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "AmPmSymbol", AttibSyb, true);
                    ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "evaluationfunction", "MaskedEditValidatorTime", true);
                    ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "LastMaskPosition", LastMaskPosition.ToString(CultureInfo.InvariantCulture), true);
                    break;
                }
                }
            }
        }
Ejemplo n.º 2
0
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (EnableClientScript)
            {
                var maskExt = (MaskedEditExtender)FindControl(ControlExtender);
                var target  = (TextBox)maskExt.FindControl(ControlToValidate);

                int firstMaskPos     = -1;
                int lastMaskPosition = -1;

                if (maskExt.ClearMaskOnLostFocus)
                {
                    firstMaskPos     = 0;
                    lastMaskPosition = MaskedEditCommon.GetValidMask(maskExt.Mask).Length + 1;
                }
                else
                {
                    firstMaskPos     = MaskedEditCommon.GetFirstMaskPosition(maskExt.Mask);
                    lastMaskPosition = MaskedEditCommon.GetLastMaskPosition(maskExt.Mask) + 1;
                }

                ScriptManager.RegisterExpandoAttribute(this, ClientID, "IsMaskedEdit", true.ToString().ToLower(CultureInfo.InvariantCulture), true);
                ScriptManager.RegisterExpandoAttribute(this, ClientID, "ValidEmpty", IsValidEmpty.ToString().ToLower(CultureInfo.InvariantCulture), true);
                ScriptManager.RegisterExpandoAttribute(this, ClientID, "MaximumValue", MaximumValue, true);
                ScriptManager.RegisterExpandoAttribute(this, ClientID, "MinimumValue", MinimumValue, true);
                ScriptManager.RegisterExpandoAttribute(this, ClientID, "InitialValue", InitialValue, true);
                ScriptManager.RegisterExpandoAttribute(this, ClientID, "ValidationExpression", ValidationExpression, true);
                ScriptManager.RegisterExpandoAttribute(this, ClientID, "ClientValidationFunction", ClientValidationFunction, true);
                ScriptManager.RegisterExpandoAttribute(this, ClientID, "TargetValidator", target.ClientID, true);
                ScriptManager.RegisterExpandoAttribute(this, ClientID, "EmptyValueMessage", EmptyValueMessage, true);
                ScriptManager.RegisterExpandoAttribute(this, ClientID, "EmptyValueText", EmptyValueBlurredText, true);
                ScriptManager.RegisterExpandoAttribute(this, ClientID, "MaximumValueMessage", MaximumValueMessage, true);
                ScriptManager.RegisterExpandoAttribute(this, ClientID, "MaximumValueText", MaximumValueBlurredMessage, true);
                ScriptManager.RegisterExpandoAttribute(this, ClientID, "MinimumValueMessage", MinimumValueMessage, true);
                ScriptManager.RegisterExpandoAttribute(this, ClientID, "MinimumValueText", MinimumValueBlurredText, true);
                ScriptManager.RegisterExpandoAttribute(this, ClientID, "InvalidValueMessage", InvalidValueMessage, true);
                ScriptManager.RegisterExpandoAttribute(this, ClientID, "InvalidValueText", InvalidValueBlurredMessage, true);
                ScriptManager.RegisterExpandoAttribute(this, ClientID, "InvalidValueCssClass", maskExt.OnInvalidCssClass, true);
                ScriptManager.RegisterExpandoAttribute(this, ClientID, "CssBlurNegative", maskExt.OnBlurCssNegative, true);
                ScriptManager.RegisterExpandoAttribute(this, ClientID, "CssFocus", maskExt.OnFocusCssClass, true);
                ScriptManager.RegisterExpandoAttribute(this, ClientID, "CssFocusNegative", maskExt.OnFocusCssNegative, true);
                ScriptManager.RegisterExpandoAttribute(this, ClientID, "TooltipMessage", TooltipMessage, true);
                ScriptManager.RegisterExpandoAttribute(this, ClientID, "FirstMaskPosition", firstMaskPos.ToString(CultureInfo.InvariantCulture), true);

                if (!String.IsNullOrEmpty(maskExt.CultureName) && maskExt.OverridePageCulture)
                {
                    ControlCulture = CultureInfo.GetCultureInfo(maskExt.CultureName);
                }
                else
                {
                    ControlCulture = CultureInfo.CurrentCulture;
                }

                switch (maskExt.MaskType)
                {
                case MaskedEditType.None: {
                    ScriptManager.RegisterExpandoAttribute(this, ClientID, "evaluationfunction", "MaskedEditValidatorNone", true);
                    ScriptManager.RegisterExpandoAttribute(this, ClientID, "LastMaskPosition", lastMaskPosition.ToString(CultureInfo.InvariantCulture), true);
                    break;
                }

                case MaskedEditType.Number: {
                    var attibCu = ControlCulture.NumberFormat.CurrencySymbol;
                    var attibDc = ControlCulture.NumberFormat.CurrencyDecimalSeparator;
                    var attibTh = ControlCulture.NumberFormat.CurrencyGroupSeparator;

                    if (maskExt.DisplayMoney != MaskedEditShowSymbol.None)
                    {
                        lastMaskPosition += maskExt.CultureCurrencySymbolPlaceholder.Length + 1;
                    }

                    if (maskExt.AcceptNegative != MaskedEditShowSymbol.None)
                    {
                        if (maskExt.DisplayMoney != MaskedEditShowSymbol.None)
                        {
                            lastMaskPosition++;
                        }
                        else
                        {
                            lastMaskPosition += 2;
                        }
                    }

                    ScriptManager.RegisterExpandoAttribute(this, ClientID, "Money", attibCu, true);
                    ScriptManager.RegisterExpandoAttribute(this, ClientID, "Decimal", attibDc, true);
                    ScriptManager.RegisterExpandoAttribute(this, ClientID, "Thousands", attibTh, true);
                    ScriptManager.RegisterExpandoAttribute(this, ClientID, "evaluationfunction", "MaskedEditValidatorNumber", true);
                    ScriptManager.RegisterExpandoAttribute(this, ClientID, "LastMaskPosition", lastMaskPosition.ToString(CultureInfo.InvariantCulture), true);
                    break;
                }

                case MaskedEditType.DateTime: {
                    //date
                    var attibSep = ControlCulture.DateTimeFormat.DateSeparator;
                    var arrDate  = ControlCulture.DateTimeFormat.ShortDatePattern.Split(new string[] { ControlCulture.DateTimeFormat.DateSeparator }, StringSplitOptions.None);

                    var attibFmt = arrDate[0].Substring(0, 1).ToUpper(ControlCulture);
                    attibFmt += arrDate[1].Substring(0, 1).ToUpper(ControlCulture);
                    attibFmt += arrDate[2].Substring(0, 1).ToUpper(ControlCulture);

                    attibFmt = (maskExt.UserDateFormat == MaskedEditUserDateFormat.None ? attibFmt : maskExt.UserDateFormat.ToString());

                    ScriptManager.RegisterExpandoAttribute(this, ClientID, "DateSeparator", attibSep, true);
                    ScriptManager.RegisterExpandoAttribute(this, ClientID, "DateFormat", attibFmt, true);
                    ScriptManager.RegisterExpandoAttribute(this, ClientID, "Century", maskExt.Century.ToString(CultureInfo.InvariantCulture), true);
                    //time
                    attibSep = ControlCulture.DateTimeFormat.TimeSeparator;
                    var attibSyb = String.Empty;
                    if (String.IsNullOrEmpty(ControlCulture.DateTimeFormat.AMDesignator + ControlCulture.DateTimeFormat.PMDesignator))
                    {
                        attibSyb = String.Empty;
                    }
                    else
                    {
                        attibSyb = ControlCulture.DateTimeFormat.AMDesignator + ";" + ControlCulture.DateTimeFormat.PMDesignator;
                    }

                    attibSyb = (maskExt.UserTimeFormat == MaskedEditUserTimeFormat.None ? attibSyb : String.Empty);

                    if (maskExt.AcceptAMPM)
                    {
                        if (!String.IsNullOrEmpty(attibSyb))
                        {
                            var sep    = Char.Parse(attibSep);
                            var arrSyb = attibSyb.Split(sep);
                            lastMaskPosition += arrSyb[0].Length + 1;
                        }
                    }

                    ScriptManager.RegisterExpandoAttribute(this, ClientID, "TimeSeparator", attibSep, true);
                    ScriptManager.RegisterExpandoAttribute(this, ClientID, "AmPmSymbol", attibSyb, true);
                    ScriptManager.RegisterExpandoAttribute(this, ClientID, "evaluationfunction", "MaskedEditValidatorDateTime", true);
                    ScriptManager.RegisterExpandoAttribute(this, ClientID, "LastMaskPosition", lastMaskPosition.ToString(CultureInfo.InvariantCulture), true);
                    break;
                }

                case MaskedEditType.Date: {
                    var attibSep = ControlCulture.DateTimeFormat.DateSeparator;
                    var arrDate  = ControlCulture.DateTimeFormat.ShortDatePattern.Split(new string[] { ControlCulture.DateTimeFormat.DateSeparator }, StringSplitOptions.None);

                    var attibFmt = arrDate[0].Substring(0, 1).ToUpper(ControlCulture);
                    attibFmt += arrDate[1].Substring(0, 1).ToUpper(ControlCulture);
                    attibFmt += arrDate[2].Substring(0, 1).ToUpper(ControlCulture);

                    attibFmt = (maskExt.UserDateFormat == MaskedEditUserDateFormat.None ? attibFmt : maskExt.UserDateFormat.ToString());

                    ScriptManager.RegisterExpandoAttribute(this, ClientID, "DateSeparator", attibSep, true);
                    ScriptManager.RegisterExpandoAttribute(this, ClientID, "DateFormat", attibFmt, true);
                    ScriptManager.RegisterExpandoAttribute(this, ClientID, "Century", maskExt.Century.ToString(CultureInfo.InvariantCulture), true);
                    ScriptManager.RegisterExpandoAttribute(this, ClientID, "evaluationfunction", "MaskedEditValidatorDate", true);
                    ScriptManager.RegisterExpandoAttribute(this, ClientID, "LastMaskPosition", lastMaskPosition.ToString(CultureInfo.InvariantCulture), true);
                    break;
                }

                case MaskedEditType.Time: {
                    var attibSep = ControlCulture.DateTimeFormat.TimeSeparator;
                    var attibSyb = String.Empty;
                    if (String.IsNullOrEmpty(ControlCulture.DateTimeFormat.AMDesignator + ControlCulture.DateTimeFormat.PMDesignator))
                    {
                        attibSyb = String.Empty;
                    }
                    else
                    {
                        attibSyb = ControlCulture.DateTimeFormat.AMDesignator + ";" + ControlCulture.DateTimeFormat.PMDesignator;
                    }

                    attibSyb = (maskExt.UserTimeFormat == MaskedEditUserTimeFormat.None ? attibSyb : String.Empty);

                    if (maskExt.AcceptAMPM)
                    {
                        if (!String.IsNullOrEmpty(attibSyb))
                        {
                            var sep    = Char.Parse(attibSep);
                            var arrSyb = attibSyb.Split(sep);
                            lastMaskPosition += arrSyb[0].Length + 1;
                        }
                    }

                    ScriptManager.RegisterExpandoAttribute(this, ClientID, "TimeSeparator", attibSep, true);
                    ScriptManager.RegisterExpandoAttribute(this, ClientID, "AmPmSymbol", attibSyb, true);
                    ScriptManager.RegisterExpandoAttribute(this, ClientID, "evaluationfunction", "MaskedEditValidatorTime", true);
                    ScriptManager.RegisterExpandoAttribute(this, ClientID, "LastMaskPosition", lastMaskPosition.ToString(CultureInfo.InvariantCulture), true);
                    break;
                }
                }
            }
        }