1

这是我的表单,其中包含邮件和电话两个条目以及发送按钮

<StackLayout HorizontalOptions="FillAndExpand" Orientation="Vertical" Grid.Row="0"  Margin="0,5,0,5">
    <Label>
        <Label.FormattedText>
            <FormattedString>
                <Span  FontSize="16" Text="Phone" TextColor="#07987f"></Span>
                <Span  FontSize="16" Text=": " TextColor="#07987f"></Span>
            </FormattedString>
        </Label.FormattedText>
    </Label>
    <Entry x:Name="entryPhone" Placeholder="Phone" Text="{Binding Phone}">
        <Entry.Behaviors>
            <xct:TextValidationBehavior x:Name="phoneValidationRule" Flags="ValidateOnUnfocusing" RegexPattern="^07[0-9]{7}$"/>
        </Entry.Behaviors>
    </Entry>
</StackLayout>

<StackLayout HorizontalOptions="FillAndExpand" Orientation="Vertical" Grid.Row="1" Margin="0,5,0,5">
    <Label>
        <Label.FormattedText>
            <FormattedString>
                <Span  FontSize="16" Text="E-mail" TextColor="#07987f"></Span>
                <Span  FontSize="16" Text=": " TextColor="#07987f"></Span>
            </FormattedString>
        </Label.FormattedText>
    </Label>
    <Entry x:Name="entryEmail" Placeholder="Email" Text="{Binding Email}">
        <Entry.Behaviors>
            <xct:EmailValidationBehavior x:Name="emailValidationRule"  Flags="ValidateOnUnfocusing"/>
        </Entry.Behaviors>
    </Entry>
</StackLayout>

<Button Text="SEND" Clicked="ValidationFormForRegistraitonClicked" TextColor="#07987f"  BackgroundColor="#eeeeee"  Margin="0,10,0,10"></Button>
<StackLayout>
    <Label IsVisible="{Binding IsValidMail}" Text="ENTER VALID EMAIL"></Label>
    <Label  IsVisible="{Binding IsValidPhone}" Text="ENTER VALID PHONE"></Label>
</StackLayout>

这是我的验证功能:

       private void ValidationFormForRegistraitonClicked(object sender, EventArgs e)
        {

                if (!emailValidationRule.IsValid)
                    viewModel.IsValidMail = true;
                else
                    viewModel.IsValidMail = false;
                if (!phoneValidationRule.IsValid)
                    viewModel.IsValidPhone = true;
                else
                    viewModel.IsValidPhone = false;              
        }

当条目为空且有效声明且错误时,我遇到问题。我有一个简单的要求。如果输入有效的地址和有效的电话通过它。如何解决这个问题?

4

1 回答 1

2

对于电话,您可以通过将Flags属性值更改为来更改应用行为的时间"ValidateOnAttaching"

<Entry x:Name="entryPhone" Placeholder="Phone" Text="{Binding Phone}">
      <Entry.Behaviors>
           <xct:TextValidationBehavior x:Name="phoneValidationRule" Flags="ValidateOnAttaching" RegexPattern="^07[0-9]{7}$"/>
      </Entry.Behaviors>
</Entry>

对于电话,您可以使用MinimumLength他的评论中提到的 GeraldVersluis

<Entry x:Name="entryEmail" Placeholder="Email" Text="{Binding Email}">
       <Entry.Behaviors>
             <xct:EmailValidationBehavior x:Name="emailValidationRule"  Flags="ValidateOnAttaching" MinimumLength="1"/>
        </Entry.Behaviors>
</Entry>

如果在某些情况下这还不够,并且您希望对“何时”进行验证进行更多控制,则可以调用ForceValidate()方法,例如在ValidationFormForRegistraitonClicked()事件处理程序内部强制验证/重新评估,或者在您认为有必要时调用。

private void ValidationFormForRegistraitonClicked(object sender, EventArgs e)
{
            emailValidationRule.ForceValidate();
            phoneValidationRule.ForceValidate();

            if (!emailValidationRule.IsValid)
                 viewModel.IsValidMail = true;
            else
                viewModel.IsValidMail = false;
            if (!phoneValidationRule.IsValid)
                viewModel.IsValidPhone = true;
            else
                viewModel.IsValidPhone = false;              
}
于 2021-02-08T14:31:40.747 回答