Forum Xamarin.Forms

Multiple behaviors on an entry

nthnth Member ✭✭
Hello guys

I’ve had a lot of trouble implementing multiple behaviors on a entry

I want to implement user input validation if the user type between integers, decimal numbers, date and text

Hope you can help or send examples

Best Answer

  • jezhjezh Member, Xamarin Team Xamurai
    Accepted Answer

    Yes, you can use Behavior to achieve this.

    NumericValidationBehavior. cs

      public class NumericValidationBehavior : Behavior<Entry>
        {
            protected override void OnAttachedTo (Entry entry)
            {
                entry.TextChanged += OnEntryTextChanged;
                base.OnAttachedTo (entry);
            }
    
            protected override void OnDetachingFrom (Entry entry)
            {
                entry.TextChanged -= OnEntryTextChanged;
                base.OnDetachingFrom (entry);
            }
    
            void OnEntryTextChanged (object sender, TextChangedEventArgs args)
            {
                string input = args.NewTextValue;
    
                bool isValid = false;
    
                double result;
                bool isValid_double = double.TryParse (input, out result);
    
                int result_int;
                bool isValid_int = int.TryParse(input, out result_int);
    
                DateTime result_date;
                bool isValid_date = DateTime.TryParse(input, out result_date);
    
    
                if (isValid_double || isValid_int|| isValid_date) {
                    isValid = true;
                }
    
                ((Entry)sender).TextColor = isValid ? Color.Default : Color.Red;
            }
        }
    

    A usage example

      <StackLayout Padding="10,50,10,0">
        <Label Text="Red when the number isn't valid" FontSize="Small" />
        <Entry Placeholder="Enter a System.Double">
            <Entry.Behaviors>
                <local:NumericValidationBehavior />
            </Entry.Behaviors>
        </Entry>
    </StackLayout>
    

    The result is:

Answers

  • jezhjezh Member, Xamarin Team Xamurai
    Accepted Answer

    Yes, you can use Behavior to achieve this.

    NumericValidationBehavior. cs

      public class NumericValidationBehavior : Behavior<Entry>
        {
            protected override void OnAttachedTo (Entry entry)
            {
                entry.TextChanged += OnEntryTextChanged;
                base.OnAttachedTo (entry);
            }
    
            protected override void OnDetachingFrom (Entry entry)
            {
                entry.TextChanged -= OnEntryTextChanged;
                base.OnDetachingFrom (entry);
            }
    
            void OnEntryTextChanged (object sender, TextChangedEventArgs args)
            {
                string input = args.NewTextValue;
    
                bool isValid = false;
    
                double result;
                bool isValid_double = double.TryParse (input, out result);
    
                int result_int;
                bool isValid_int = int.TryParse(input, out result_int);
    
                DateTime result_date;
                bool isValid_date = DateTime.TryParse(input, out result_date);
    
    
                if (isValid_double || isValid_int|| isValid_date) {
                    isValid = true;
                }
    
                ((Entry)sender).TextColor = isValid ? Color.Default : Color.Red;
            }
        }
    

    A usage example

      <StackLayout Padding="10,50,10,0">
        <Label Text="Red when the number isn't valid" FontSize="Small" />
        <Entry Placeholder="Enter a System.Double">
            <Entry.Behaviors>
                <local:NumericValidationBehavior />
            </Entry.Behaviors>
        </Entry>
    </StackLayout>
    

    The result is:

  • nthnth Member ✭✭

    thanks for your reply. It works, but I would also like to be able to write normal letters where the text does not turn red. How?

  • jezhjezh Member, Xamarin Team Xamurai

    You mean a string made up entirely of letters?

  • nthnth Member ✭✭

    311/5000
    I want my text box to validate on both string, decimal, date and integer. So if I type in text it should be possible with a normal keyboard and numbers the numeric keypad should appear.

    Then I would like my button to be turned off depending on whether it is the right user input

  • nthnth Member ✭✭

    is it possible to make conditions such as something is enabled from the backend database and then validate?

  • nthnth Member ✭✭
  • jezhjezh Member, Xamarin Team Xamurai

    I want my text box to validate on both string, decimal, date and integer.

    Do you have any rules for the string you mentioned?

    BTW, any input we get from the Entey is initially in the form of a string.

  • nthnth Member ✭✭

    No i dont have any rules for the string i mentioned, but i dont want the text color to be red when i type text...
    @jezh

  • jezhjezh Member, Xamarin Team Xamurai

    Then if the input is not the type you need (decimal, date and integer), how do you want to prompt?

    If you want to change other property of this entry, you can just change the following code:

     ((Entry)sender).TextColor = isValid ? Color.Default : Color.Red;
    
Sign In or Register to comment.