在xamarin表单中使用xamarin社区工具包确认/匹配密码验证

omjgkv6w  于 2023-08-01  发布在  其他
关注(0)|答案(1)|浏览(78)

如何创建确认/匹配密码验证。我正在使用Xamarin表单中的Xamarin社区工具包进行其他验证

<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>

字符串
在谷歌上搜索了一下

vlf7wbxs

vlf7wbxs1#

你可以通过下面的代码来实现:

<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>

字符串
有关更多信息,您可以参考官方代码示例:MultiValidationBehaviorPage.xaml的数据。

更新:

根据你的需要,我编辑了上面的代码。我补充一点:

<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'}"/>

相关问题