【发布时间】:2017-09-10 03:53:05
【问题描述】:
我创建了两个用户输入字段,即用户名和密码。
这是我的 Xaml 代码:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:LoginUser"
x:Class="LoginUser.MainPage">
<StackLayout Spacing="20" Padding="50" VerticalOptions="Center">
<Entry x:Name="txtUsername" Placeholder="Username"></Entry>
<Entry x:Name="txtPassword" Placeholder="Password" IsPassword="True">
<Entry.Behaviors>
<local:ValidationBehavior/>
</Entry.Behaviors>
</Entry>
<Button Text="Log In" TextColor="White" BackgroundColor="##ff77D065" Clicked="Button_Onclick"></Button>
</StackLayout>
</ContentPage>
这是我的 ValidationBehaviour.cs 文件代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using System.Text.RegularExpressions;
using UIKit;
namespace LoginUser
{
public class ValidationBehavior: Behavior<Entry>
{
const string pwRegex = @"^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%*#?&])[A-Za-z\d$@$!%*#?&]{8,}$";
protected override void OnAttachedTo(Entry bindable)
{
bindable.TextChanged += HandleTextChanged;
base.OnAttachedTo(bindable);
}
void HandleTextChanged(object sender, TextChangedEventArgs e)
{
bool IsValid = false;
IsValid = (Regex.IsMatch(e.NewTextValue, pwRegex));
((Entry)sender).TextColor = IsValid ? Color.Default : Color.Red;
}
protected override void OnDetachingFrom(Entry bindable)
{
bindable.TextChanged -= HandleTextChanged;
base.OnDetachingFrom(bindable);
}
}
}
如果从文本框中删除任何字符串字符,如小写、大写、特殊字符,我想显示显示警报。
【问题讨论】:
-
这与您之前的问题有何不同?您不了解如何使用 DisplayAlert?或者您是在问如何检测文本是否已更改?
-
例如,我在密码字段中提供了 App@1234 之类的密码,所以我的问题是如果我从密码字段中删除字符串 char,例如“A”,那么应该会发生错误。跨度>
-
TextChangedEventArgs 为您提供新值和旧值。您需要比较它们并提出符合您要求的算法来测试错误。我们无法为您做到这一点。
-
这是你第三次问基本相同的问题了。
标签: c# xamarin.forms