Xamarin generates a password and confirms password confirmation

I have an application Xamarin Formsin which I have a registration form in which I need to check the password and the Confirm Password password should be the same.

Is there any way to implement this with Xamarin Behaviour?

I have already done Xamarin Behaviourfor Required Fieldvalidation and Email Regexvalidation as below -

public class RequiredValidatorBehavior : Behavior<Entry>
    {
        static readonly BindablePropertyKey IsValidPropertyKey = BindableProperty.CreateReadOnly("IsValid", typeof(bool), typeof(RequiredValidatorBehavior), false);
        static readonly BindableProperty IsValidProperty = IsValidPropertyKey.BindableProperty;

        public bool IsValid
        {
            get { return (bool)base.GetValue(IsValidProperty); }
            private set { base.SetValue(IsValidPropertyKey, value); }
        }

        protected override void OnAttachedTo(Entry bindable)
        {
            bindable.Unfocused += HandleFocusChanged;
            base.OnAttachedTo(bindable);
        }
        protected override void OnDetachingFrom(Entry bindable)
        {
            bindable.Unfocused -= HandleFocusChanged;
            base.OnDetachingFrom(bindable);
        }
        void HandleFocusChanged(object sender, FocusEventArgs e)
        {
            IsValid = !string.IsNullOrEmpty (((Entry)sender).Text);
        }
    }

Implementation Behaviourin XAML Content Page-

<Entry x:Name="password" Placeholder="New Password">
    <Entry.Behaviors>
        <local:RequiredValidatorBehavior x:Name="passwordValidator"/>   
    </Entry.Behaviors>
</Entry>

The problem is that I am new to development Xamarinand have no idea how I can get the value of the fields Passwordand Confirm Passwordin Behaviourso that I can compare them. I do not want to compare them when I click the button when submitting the form, the fields should be compared when the user enters the fields. Any code, help or guidance is noticeable.

+4
3

https://forums.xamarin.com/discussion/34695/xaml-how-do-you-pass-a-control-view-reference-into-a-behavior

. Compare Validator , .

<behaviors:CompareValidator x:Name="ComparePasswordsValidator" 
        CompareToEntry="{Binding Source={x:Reference PasswordEntry}}" /> 

:

XAML -

<Entry x:Name="password" Placeholder="New Password" IsPassword="true">
    <Entry.Behaviors>
        <local:RequiredValidatorBehavior x:Name="passwordValidator"/>   
    </Entry.Behaviors>
</Entry>
<Entry x:Name="confirmPassword" Placeholder="Confirm Password" IsPassword="true">
    <Entry.Behaviors>
        <local:ConfirmPasswordBehavior x:Name="confirmPasswordBehavior" CompareToEntry="{Binding Source={x:Reference password}}" />
    </Entry.Behaviors>
</Entry>

-

    public class ConfirmPasswordBehavior : Behavior<Entry>
    {
        static readonly BindablePropertyKey IsValidPropertyKey = BindableProperty.CreateReadOnly("IsValid", typeof(bool), typeof(ConfirmPasswordBehavior), false);
        public static readonly BindableProperty IsValidProperty = IsValidPropertyKey.BindableProperty;

        public static readonly BindableProperty CompareToEntryProperty = BindableProperty.Create("CompareToEntry", typeof(Entry), typeof(ConfirmPasswordBehavior), null);

        public Entry CompareToEntry
        {
            get { return (Entry)base.GetValue(CompareToEntryProperty); }
            set { base.SetValue(CompareToEntryProperty, value); }
        }
        public bool IsValid
        {
            get { return (bool)base.GetValue(IsValidProperty); }
            private set { base.SetValue(IsValidPropertyKey, value); }
        }
        protected override void OnAttachedTo(Entry bindable)
        {
            bindable.TextChanged += HandleTextChanged;
            base.OnAttachedTo(bindable);
        }
        protected override void OnDetachingFrom(Entry bindable)
        {
            bindable.TextChanged -= HandleTextChanged;
            base.OnDetachingFrom(bindable);
        }
        void HandleTextChanged(object sender, TextChangedEventArgs e)
        {
            var password = CompareToEntry.Text;
            var confirmPassword = e.NewTextValue;
            IsValid = password.Equals (confirmPassword);
        }
    }
+8

Punnet:

-, null HandleTextChanged, ,

    void HandleTextChanged(object sender, TextChangedEventArgs e)
    {
        string theBase = CompareToEntry.Text;
        string confirmation = e.NewTextValue;
        // here is the change
        IsValid = (bool)theBase?.Equals(confirmation);

        ((Entry)sender).TextColor = IsValid ? Color.Green : Color.Red;
    }

, .

    void baseValue_changed(object sender, TextChangedEventArgs e)
    {
        IsValid = (bool)((Entry)sender).Text?.Equals(thisEntry.Text);
        thisEntry.TextColor = IsValid ? Color.Green : Color.Red;
    }

,

public class ComparisonBehavior : Behavior<Entry>
{
    private Entry thisEntry;

    static readonly BindablePropertyKey IsValidPropertyKey = BindableProperty.CreateReadOnly("IsValid", typeof(bool), typeof(ComparisonBehavior), false);
    public static readonly BindableProperty IsValidProperty = IsValidPropertyKey.BindableProperty;

    public static readonly BindableProperty CompareToEntryProperty = BindableProperty.Create("CompareToEntry", typeof(Entry), typeof(ComparisonBehavior), null);

    public Entry CompareToEntry
    {
        get { return (Entry)base.GetValue(CompareToEntryProperty); }
        set
        {
            base.SetValue(CompareToEntryProperty, value);
            if (CompareToEntry != null)
                CompareToEntry.TextChanged -= baseValue_changed;
            value.TextChanged += baseValue_changed;
        }
    }

    void baseValue_changed(object sender, TextChangedEventArgs e)
    {
        IsValid = ((Entry)sender).Text.Equals(thisEntry.Text);
        thisEntry.TextColor = IsValid ? Color.Green : Color.Red;
    }


    public bool IsValid
    {
        get { return (bool)base.GetValue(IsValidProperty); }
        private set { base.SetValue(IsValidPropertyKey, value); }
    }
    protected override void OnAttachedTo(Entry bindable)
    {
        thisEntry = bindable;

        if (CompareToEntry != null)
            CompareToEntry.TextChanged += baseValue_changed;

        bindable.TextChanged += HandleTextChanged;
        base.OnAttachedTo(bindable);
    }

    protected override void OnDetachingFrom(Entry bindable)
    {
        bindable.TextChanged -= HandleTextChanged;
        if (CompareToEntry != null)
            CompareToEntry.TextChanged -= baseValue_changed;
        base.OnDetachingFrom(bindable);
    }

    void HandleTextChanged(object sender, TextChangedEventArgs e)
    {
        string theBase = CompareToEntry.Text;
        string confirmation = e.NewTextValue;
        IsValid = (bool)theBase?.Equals(confirmation);

        ((Entry)sender).TextColor = IsValid ? Color.Green : Color.Red;
    }
}
+3

.

URL .

https://github.com/MaulikParmar/XamarinForms/tree/master/ValidationDemo

8.1.

Here is the youtube url for describing the project and making better use of this control.

https://www.youtube.com/watch?v=eEi-Oky4U08

+2
source

Source: https://habr.com/ru/post/1598353/


All Articles