Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

visual studio 2015 - Login page is not validating in xamarin form

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

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>

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

    }
}

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

With password: 123456

enter image description here

With password: AbcDe12!

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...