Search code examples
c#validationxamarin.formsrestriction

Insert data to Firebase and set restriction to UI(username and password) in xamarin form


I have create acc page in xamarin form. The fields are basic info for customer like firstname, lastname and I have field "username" and "password". So it goes like this. I want to set a rule for;

  1. Username; -no duplication for username. Because every username inputted by customer, It check in database if that username is avail or not. Example, username: dummy123

I want this way for username; -upon tapping the entry field for username, and assume customer will input "dummy123" a toast will appear like "username already exist"

  1. Password; -password must contain; capital letter, small char, special char, number and 8 letter and above.

I want this way for password; -upon tapping the entry field for password, and assume customer will input "@imcool123" a toast will appear like this; "password must contain capital letter, small char, special char, number and 8 letter and above"

any link posted will be appreciated. Thank you so much.


Solution

  • You can follow the steps below to make your ideas work.

    First, you can create a model for deserializing your response.

     public class Person  
        {  
            public string Name { get; set; }  
        }  
    

    Second, you can write the code to connect the Firebase Realtime Database.

    using Firebase.Database;  
    using Firebase.Database.Query;  
      
    FirebaseClient firebase = new FirebaseClient("https://");  
    

    Third, create an Entry with TextChanged in the MainPage.xaml.

     <StackLayout>
            <Entry x:Name="EntryText" TextChanged="EntryText_TextChanged" />
     </StackLayout>
    

    Fourth, fulfill the EntryText_TextChanged method in MainPage.xaml.cs. The method provide a way to match the data which in the Firebase.

     public partial class MainPage : ContentPage
        {
            public MainPage()
            {
                InitializeComponent();
            }
     
     public async Task<List<Person>> GetAllPersons()  
        {  
    
            return (await firebase  
              .Child("Persons")  
              .OnceAsync<Person>()).Select(item => new Person  
              {  
                  Name = item.Object.Name,  
                  PersonId = item.Object.PersonId  
              }).ToList();  
        }  
    
            private void EntryText_TextChanged(object sender, TextChangedEventArgs e)
            {
                var oldText = e.OldTextValue;
                var newText = e.NewTextValue;
    
                var person = await firebaseHelper.GetPerson(Convert.ToString(EntryText.Text);
                if (person != null)
                {
                    
                    EntryText.Text = person.Name;
                    // this is a Toast method
                    await DependencyService.Get<IToast>().ShortAlert("username already exist");
    
                }
                else
                {
                    
                }
            }
    // This is the method to find the name if it has existed
            public async Task<Person> GetPerson(string personName)
            {
                var allPersons = await GetAllPersons();
                await firebase
                  .Child("Persons")
                  .OnceAsync<Person>();
                return allPersons.Where(a => a.PersonName == personName).FirstOrDefault();
            }
        }
    

    In addition, here is the information about the Xamarin.Forms - How To Make Toast Message Using Dependency Service. You can check this article.