登录页面未以xamarin格式验证

时间:2017-09-06 12:36:51

标签: visual-studio-2015 xamarin.forms

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

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)(?=.*[$@$!%*#?&])[A-Za- z\d$@$!%*#?&]{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模拟器中运行程序时,登录设计工作正常,但验证无效。

1 个答案:

答案 0 :(得分: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}$

使用我的样本你会得到这样的结果:

使用密码: 123456

enter image description here

使用密码: AbcDe12!

enter image description here