登录页面未以 xamarin 形式进行验证 [英] Login page is not validating in xamarin form

查看:30
本文介绍了登录页面未以 xamarin 形式进行验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created login page using xamarin forms and created the class file called validationbehaviour.cs and validated input fields through behaviour class.

xaml 登录页面代码:

xaml login page code:

<?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代码:

Here is my validationbehaviour.cs code:

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- zd$@$!%*#?&]{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 模拟器中运行程序时,登录设计工作正常但验证不起作用.

So when I run the program in android emulator,the login design is working fine but validation is not working.

推荐答案

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

Maybe you should change IsValid variable to property like this:

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

    }
}

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

Another error is also your regular expression, try the following:

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

如果您还想要求至少一个特殊字符(这可能是个好主意),请尝试以下操作:

If you also want to require at least one special character (which is probably a good idea), try this:

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

使用我的示例,您会得到这样的结果:

With my sample you get like this:

使用密码:123456

使用密码:AbcDe12!

这篇关于登录页面未以 xamarin 形式进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆