Home > Net >  Email validation getting System.ArgumentNullException: 'Value cannot be null
Email validation getting System.ArgumentNullException: 'Value cannot be null

Time:02-23

when the entry box is empty and I pressed the button. I get this error message System.ArgumentNullException: 'Value cannot be null. Parameter name: input' I tried many ways to figure out

        public MainPage()
    {
        InitializeComponent();
    }
    protected override async void OnAppearing()
    {
        base.OnAppearing();
        listmembers.ItemsSource = await App.Database.GetMemberAsync();
    }

    private async void Button_Clicked(object sender, EventArgs e)
    {

        var email = loginEmail.Text;
        var emailpattern = @"^([\w\.\-] )@([\w\-] )((\.(\w){2,3}) )$";


        if (Regex.IsMatch(email, emailpattern))
        {
            await App.Database.SaveMemberAsync(new Member
            {
                Name = loginEmail.Text,
            });
            loginEmail.Text =  string.Empty;
            listmembers.ItemsSource = await 
                App.Database.GetMemberAsync();
        }

        else
        {
            await DisplayAlert("Error", "Invaild Email", "ok");
        }
    }
}

CodePudding user response:

you need to test for null

if (!string.IsNullOrEmpty(email) && Regex.IsMatch(email, emailpattern))
  • Related