mirror of
https://github.com/bitwarden/android.git
synced 2024-11-01 15:45:42 +03:00
39 lines
1.5 KiB
C#
39 lines
1.5 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 entry)
|
|
{
|
|
entry.TextChanged += HandleTextChanged;
|
|
base.OnAttachedTo(entry);
|
|
}
|
|
|
|
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 entry)
|
|
{
|
|
entry.TextChanged -= HandleTextChanged;
|
|
base.OnDetachingFrom(entry);
|
|
}
|
|
}
|
|
}
|