2017-09-06 8 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コードです:私は、Androidエミュレータでプログラムを実行すると

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

    } 
} 

だから、ログインデザインが正常に動作しているが、検証があります働いていない。

+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}$ 

あなたはまた、少なくとも1を必要とする場合特殊文字(これはおそらく良い考えです)を試してみてください:

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

Wi

パスワード付き:パスワード付き

enter image description here

AbcDe12私のサンプル番目のあなたは、このように取得します!

enter image description here

+0

ありがとう、ウィルソン。うまくいきます。 – Sridhar

関連する問題