0

How to create a confirm/Match password validation. I am using xamarin community toolkit in xamarin forms for other validations

<Entry x:Name="passwordEntry" Placeholder="Password" HorizontalOptions="FillAndExpand" 
                               VerticalOptions="Fill" IsPassword="True" Margin="0,5" PlaceholderColor="LightGray">
                                <Entry.Behaviors>
                                    <xct:MultiValidationBehavior>
                                        <xct:TextValidationBehavior MinimumLength="5"/>
                                        <xct:CharactersValidationBehavior CharacterType="Digit" MaximumCharacterCount="1"/>
                                        <xct:CharactersValidationBehavior CharacterType="LowercaseLatinLetter" MaximumCharacterCount="1"/>
                                        <xct:CharactersValidationBehavior CharacterType="UppercaseLatinLetter" MaximumCharacterCount="1"/>
                                        <xct:CharactersValidationBehavior CharacterType="NonAlphanumericSymbol" MaximumCharacterCount="1"/>
                                        <xct:CharactersValidationBehavior CharacterType="Whitespace" MaximumCharacterCount="0"/>
                                    </xct:MultiValidationBehavior>
                                </Entry.Behaviors>
                            </Entry>

                            <Entry x:Name="confermPasswordEntry" Placeholder="Conferm Password" HorizontalOptions="FillAndExpand" 
                               VerticalOptions="Fill" IsPassword="True" Margin="0,5" PlaceholderColor="LightGray">
                                
                            </Entry>

Searched on google just

Ahsan Ali
  • 19
  • 1
  • 1
    Have you tried https://learn.microsoft.com/en-us/xamarin/community-toolkit/behaviors/requiredstringvalidationbehavior – Jason Jun 27 '23 at 22:56
  • May I know if you have got any chance to check my answer? I am glad to help if you have any other questions. – Jianwei Sun - MSFT Jul 03 '23 at 08:10

1 Answers1

0

You can achieve it by the following code:

<ContentPage ...>
    <ContentPage.Resources>
        <Style x:Key="InvalidEntryStyle" TargetType="Entry">
            <Setter Property="TextColor" Value="Red" />
        </Style>
    </ContentPage.Resources>
    <ContentPage.Content>
        <StackLayout>
            <Entry IsPassword="True" Placeholder="Password" x:Name="passwordEntry">
                <Entry.Behaviors>
                    <xct:MultiValidationBehavior InvalidStyle="{StaticResource InvalidEntryStyle}"  >
                        <xct:CharactersValidationBehavior x:Name="digit" CharacterType="Digit" MinimumCharacterCount="1" xct:MultiValidationBehavior.Error="1 digit" RegexPattern="" />
                        <xct:CharactersValidationBehavior x:Name="upper" CharacterType="UppercaseLetter" MinimumCharacterCount="1" xct:MultiValidationBehavior.Error="1 upper" RegexPattern="" />
                        <xct:CharactersValidationBehavior x:Name="lower" CharacterType="LowercaseLetter" MinimumCharacterCount="1" xct:MultiValidationBehavior.Error="1 lower" RegexPattern="" />
                        <xct:CharactersValidationBehavior x:Name="symbol" CharacterType="NonAlphanumericSymbol" MinimumCharacterCount="1" xct:MultiValidationBehavior.Error="1 symbol" RegexPattern=""  />
                        <xct:CharactersValidationBehavior x:Name="any" CharacterType="Any" MinimumCharacterCount="8" xct:MultiValidationBehavior.Error="8 char" RegexPattern="" />
                    </xct:MultiValidationBehavior>
                </Entry.Behaviors>
            </Entry>
            <Entry IsPassword="True" Placeholder="Confirm Password">
                <Entry.Behaviors>
                    <xct:RequiredStringValidationBehavior InvalidStyle="{StaticResource InvalidEntryStyle}"
                                                          RequiredString="{Binding Source={x:Reference passwordEntry},Path=Text}"
                                                          x:Name="confrim"/>
                </Entry.Behaviors>
            </Entry>
            <StackLayout Orientation="Vertical">
                <Label Text="{Binding IsValid, Source={x:Reference digit}, StringFormat='{}{0} : at least 1 digit'}"/>
                <Label Text="{Binding IsValid, Source={x:Reference upper}, StringFormat='{}{0} : at least 1 upper'}"/>
                <Label Text="{Binding IsValid, Source={x:Reference lower}, StringFormat='{}{0} : at least 1 lower'}"/>
                <Label Text="{Binding IsValid, Source={x:Reference symbol}, StringFormat='{}{0} : at least 1 symbol'}"/>
                <Label Text="{Binding IsValid, Source={x:Reference any}, StringFormat='{}{0} : at least 8 char'}"/>
                <Label Text="{Binding IsValid, Source={x:Reference confrim}, StringFormat='{}{0} : should match password'}"/>
            </StackLayout>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

For more information you can refer to the official code sample: MultiValidationBehaviorPage.xaml.

Update:

According to your need, I edit the above code. I add this:

<Entry IsPassword="True" Placeholder="Confirm Password">
    <Entry.Behaviors>
        <xct:RequiredStringValidationBehavior 
            InvalidStyle="{StaticResource InvalidEntryStyle}" 
            RequiredString="{Binding Source={x:Reference passwordEntry},Path=Text}" 
            x:Name="confrim"/>
    </Entry.Behaviors>
</Entry>
....
<Label Text="{Binding IsValid, Source={x:Reference confrim}, StringFormat='{}{0} : should match password'}"/>
Jianwei Sun - MSFT
  • 2,289
  • 1
  • 3
  • 7
  • Its not i am looking for. You can easily understand by reading my xaml what i want. I want user re_enter his password and i want to compare that confirm password field value with password field value. You totally have removed the second entry – Ahsan Ali Jul 03 '23 at 19:21
  • Sorry about that. I update the answer and wish it can help you. – Jianwei Sun - MSFT Jul 04 '23 at 02:04