Esempio n. 1
0
        protected override void OnPreviewLostKeyboardFocus(KeyboardFocusChangedEventArgs e)
        {
            var count = CountDigits();

            if (!PhoneValidator.ValidatePhoneNumber(Text, out var compactNumber))
            {
                MessageBox.Show($"Illegal phone number '{Text}'.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                e.Handled = true;
            }
            else if (count != 0 && count < MinDigits)
            {
                MessageBox.Show($"Phone number '{Text}' should have at least {MinDigits} digits.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                e.Handled = true;
            }
            else if (count > MaxDigits)
            {
                MessageBox.Show($"Phone number '{Text}' should have at most {MaxDigits} digits.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                e.Handled = true;
            }
            else
            {
                CompactNumber = compactNumber;
            }

            Text = CountryCode.Format(Text) ?? Text;
            base.OnPreviewLostKeyboardFocus(e);
        }
Esempio n. 2
0
        /// <summary>
        /// Sets initial value from code behind. If isRequired is true, user needs to change the value before saving is possible.
        /// If minDigits or maxDigits are null, MinDigits or MaxDigits value gets not changed.
        /// </summary>
        public virtual void Initialise(string?text, bool?isRequired = null, int?minDigits = null, int?maxDigits = null)
        {
            if (!PhoneValidator.ValidatePhoneNumber(text, out var compactNumber))
            {
                throw new Exception($"Error PhoneTextBox.Initialise(): Text {text} is not a valid phone number.");
            }
            var newMinDigits = minDigits ?? MinDigits;
            var newMaxDigits = maxDigits ?? MaxDigits;
            var count        = CountDigits();

            if (count != 0)
            {
                if (count < newMinDigits)
                {
                    throw new Exception($"Error PhoneTextBox.Initialise(): Phone number {Text} should have at least MinDigit {newMinDigits} digits.");
                }
                if (count > newMaxDigits)
                {
                    throw new Exception($"Error PhoneTextBox.Initialise(): Phone number {Text} should have at most MaxDigits {newMaxDigits} digits.");
                }
            }
            isInitialising = true;
            MinDigits      = newMinDigits;
            MaxDigits      = newMaxDigits;
            CompactNumber  = compactNumber;
            isInitialising = false;

            if (text != null)
            {
                text = CountryCode.Format(text) ?? text;
            }
            base.Initialise(text, isRequired);
        }
Esempio n. 3
0
        //      ---------

        protected override void OnPreviewTextInput(TextCompositionEventArgs e)
        {
            if (e.Text.Length > 0) //ctrl + key results in Text.Length==0
            {
                if (e.Text.Length != 1)
                {
                    throw new NotSupportedException($"PhoneTextBox supports only ASCII code.");
                }

                var inputChar            = e.Text[0];
                var textWithoutSelection = GetTextWithoutSelection();
                if (!PhoneValidator.ValidateUserInput(inputChar, CaretIndex, textWithoutSelection) ||
                    (inputChar >= '0' && inputChar <= '9' && CountDigits() >= MaxDigits))
                {
                    e.Handled = true;
                }
            }

            base.OnPreviewTextInput(e);
        }
Esempio n. 4
0
        protected override void OnTextBoxInitialized()
        {
            //verify the values set in XAML
            if (!PhoneValidator.ValidatePhoneNumber(Text, out var compactNumber))
            {
                throw new Exception($"Error PhoneTextBox: Text {Text} is not a valid phone number.");
            }
            Text = CountryCode.Format(Text) ?? Text;
            var count = CountDigits();

            if (count != 0)
            {
                if (count < MinDigits)
                {
                    throw new Exception($"Error PhoneTextBox: Phone number {Text} should have at least MinDigits {MinDigits} digits.");
                }
                if (count > MaxDigits)
                {
                    throw new Exception($"Error PhoneTextBox: Phone number {Text} should have at most MaxDigits {MaxDigits} digits.");
                }
            }

            CompactNumber = compactNumber;
        }