2017-09-06 20 views
2

我已经使用xamarin表单创建了登录页面,并通过行为类创建了名为validationbehaviour.cs的类文件并验证了输入字段。登录页面未通过xamarin表单验证

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 Placeholder="Username"></Entry> 
    <Entry Placeholder="Password" IsPassword="True"> 
     <Entry.Behaviors> 
     <local:PasswordValidationBehavior /> 
     </Entry.Behaviors> 
    </Entry> 
    <Button Text="Log In" TextColor="White" BackgroundColor="##ff77D065"></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; 

namespace LoginUser 
{  
    public class ValidationBehavior: Behavior<Entry> 
    {  
     const string pwRegex = @"^(?=.*[A-Za-z])(?=.*\d)(?=.*[[email protected]$!%*#?&])[A-Za- z\[email protected]$!%*#?&]{8,}$";                    
     protected override void OnAttachedTo(Entry bindable) 

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

     private 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); 
     } 

    } 
} 

所以,当我在运行Android模拟器程序,在登录的设计工作正常,但验证不工作。

+0

如果你把一个断点HandleTextChanged方法中,它得到正确叫什么名字?它返回什么? – hankide

回答

0

也许你应该IsValid的变量更改属性是这样的:

using System;   
using System.Collections.Generic;  
using System.Linq;  
using System.Text;   
using System.Threading.Tasks;  
using Xamarin.Forms;  
using System.Text.RegularExpressions; 

namespace LoginUser 
{  
    public class ValidationBehavior: Behavior<Entry> 
    {  
     const string pwRegex = @"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z]).{8,15}$";    

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

     public 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.TextChanged += HandleTextChanged; 
     } 

     private void HandleTextChanged(object sender, TextChangedEventArgs e) 
     { 
      IsValid= (Regex.IsMatch(e.NewTextValue, pwRegex, RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250))); 
      ((Entry)sender).TextColor = IsValid ? Color.Default : Color.Red; 
     } 

     protected override void OnDetachingFrom(Entry bindable) 
     {       
      bindable.TextChanged -= HandleTextChanged; 
     } 

    } 
} 

另一个错误也是你的正则表达式,请尝试以下操作:

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,15}$ 

如果你也想至少需要一个特殊字符(这可能是个好主意),试试这个:

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z]).{8,15}$ 

Wi日我的例子中,你得到这样的:

具有密码:

enter image description here

具有密码:AbcDe12!

enter image description here

+0

谢谢你wilson.Its工作正常。 – Sridhar