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

查看:174
本文介绍了登录页面未以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>

这是我的validatebehaviour.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- 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仿真器中运行该程序时,登录设计可以正常工作,但验证无法正常工作.

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天全站免登陆