mirror of
https://github.com/bitwarden/android.git
synced 2024-11-01 23:54:06 +03:00
37 lines
1.4 KiB
C#
37 lines
1.4 KiB
C#
using System;
|
|
using System.Text.RegularExpressions;
|
|
using Xamarin.Forms;
|
|
|
|
namespace Bit.App.Behaviors
|
|
{
|
|
public class EmailValidationBehavior : Behavior<Entry>
|
|
{
|
|
private const string EmailRegex = @"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
|
|
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$";
|
|
|
|
private static readonly BindablePropertyKey IsValidPropertyKey = BindableProperty.CreateReadOnly("IsValid", typeof(bool), typeof(EmailValidationBehavior), false);
|
|
public static readonly BindableProperty IsValidProperty = IsValidPropertyKey.BindableProperty;
|
|
|
|
public bool IsValid
|
|
{
|
|
get { return (bool)GetValue(IsValidProperty); }
|
|
private set { SetValue(IsValidPropertyKey, value); }
|
|
}
|
|
|
|
protected override void OnAttachedTo(Entry bindable)
|
|
{
|
|
bindable.TextChanged += HandleTextChanged;
|
|
}
|
|
|
|
void HandleTextChanged(object sender, TextChangedEventArgs e)
|
|
{
|
|
IsValid = Regex.IsMatch(e.NewTextValue, EmailRegex, RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250));
|
|
((Entry)sender).BackgroundColor = IsValid ? Color.Default : Color.Red;
|
|
}
|
|
|
|
protected override void OnDetachingFrom(Entry bindable)
|
|
{
|
|
bindable.TextChanged -= HandleTextChanged;
|
|
}
|
|
}
|
|
}
|