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