Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/egbakou/formvalidation

Real time form validation using Triggers and Converters in Xamarin.Forms
https://github.com/egbakou/formvalidation

converters forms triggers validation xamarin-forms

Last synced: about 1 month ago
JSON representation

Real time form validation using Triggers and Converters in Xamarin.Forms

Awesome Lists containing this project

README

        

## Real time form validation using Triggers and Converters in Xamarin.Forms

In this Xamarin.Forms project, we want to use **Triggers** and **Converters** to validate in real time login form.

## Demo on Youtube

[![Youtube demo](FormValidation/screenshots/demo.jpg)](https://www.youtube.com/watch?v=L6PELicCyMk)

## Create project and select Blank model

![](FormValidation/screenshots/create_project.PNG)

## Project structure at the end

![](FormValidation/screenshots/project_structure.PNG)

## Topic

The goal is to check if email(user name) and password are correct before enable login button.

- The email must match the following regular expression `^[a-z0-9._-]+@[a-z0-9._-]+\\.[a-z]{2,6}$`
- The password must have 8 characters at least.
- If email or password is invalid, login button will be disabled and error labels visible.

## Our converters

- Code for Converter that validate email address and used by trigger to enable login button:

```csharp
using System;
using System.Globalization;
using System.Text.RegularExpressions;
using Xamarin.Forms;

namespace FormValidation.Converters
{
///
/// Converter that check if username is correct and return true, otherwise return false.
///
public class UsernameCorrectConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return isUserNameCorrect(value);
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}

private bool isUserNameCorrect(object value)
{
if (value is string)
{
bool isEmail = Regex.IsMatch(
(string)value, "^[a-z0-9._-]+@[a-z0-9._-]+\\.[a-z]{2,6}$");

int length = ((string)value).Trim().Length;

if (length >= 7 && length <= 60 && isEmail)
return true;
else
return false;

}
return false;
}

}
}

```

- Code for Converter that validate email address and used by trigger to show email error label:

```csharp
using System;
using System.Globalization;
using System.Text.RegularExpressions;
using Xamarin.Forms;

namespace FormValidation.Converters
{
///
/// Converter that check if username is correct and return true, otherwise return false.
/// The idea is to be able to show or hide username error label.
///
public class UsernameCorrectToHideLabelConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return isUserNameCorrect(value);
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}

private bool isUserNameCorrect(object value)
{
if (value == null || ((string)value).Length == 0)
return true;

if (value is string)
{
bool isEmail = Regex.IsMatch(
(string)value, "^[a-z0-9._-]+@[a-z0-9._-]+\\.[a-z]{2,6}$");

int length = ((string)value).Trim().Length;
if (length >= 7 && length <= 60 && isEmail)
return true;
else
return false;

}
return false;
}
}
}

```

- Code for Converter that validate password and used by trigger to enable login button:

```csharp
using System;
using System.Globalization;
using Xamarin.Forms;

namespace FormValidation.Converters
{
///
/// Converter that check if passsword is correct and return true, otherwise return false.
///
public class PasswordCorrectConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return isPasswordCorrect(value);
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}

private bool isPasswordCorrect(object value)
{
if (value is string)
{
int length = ((string)value).Trim().Length;
if (length >= 8)
return true;
else
return false;
}
return false;
}
}
}
```

- Code for Converter that validate password and used by trigger to show password error label:

```csharp
using System;
using System.Globalization;
using Xamarin.Forms;

namespace FormValidation.Converters
{
///
/// Converter that check if password is correct and return true, otherwise return false.
/// The idea is to be able to show or hide password error label.
///
class PasswordCorrectToHideLabelConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return isPasswordCorrect(value);
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}

private bool isPasswordCorrect(object value)
{
if (value == null || ((string)value).Length == 0)
return true;

if (value is string)
{
int length = ((string)value).Trim().Length;
if (length >= 8)
return true;
else
return false;
}
return false;
}
}
}

```

## Login page

```xaml






























































```

## Code behind

```csharp
using Xamarin.Forms;

namespace FormValidation
{
public partial class FormsPage : ContentPage
{
public FormsPage()
{
InitializeComponent();
ShowLoginIndicator.IsRunning = false;
}

private void LoginBt_Clicked(object sender, System.EventArgs e)
{
ShowLoginIndicator.IsVisible = true;
ShowLoginIndicator.IsRunning = true;
}

private void UserNameEntry_Focused(object sender, FocusEventArgs e)
{
UserNameEntry.HorizontalTextAlignment = TextAlignment.Center;
}

private void UserNameEntry_Unfocused(object sender, FocusEventArgs e)
{
UserNameEntry.HorizontalTextAlignment = TextAlignment.Start;
}

private void PasswordEntry_Focused(object sender, FocusEventArgs e)
{
PasswordEntry.HorizontalTextAlignment = TextAlignment.Center;
}

private void PasswordEntry_Unfocused(object sender, FocusEventArgs e)
{
PasswordEntry.HorizontalTextAlignment = TextAlignment.Start;
}
}
}

```