How To Validate An Email Address In Xamarin Android App Using Visual Studio 2015

Introduction

Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows phone, Android, iOS). In Xamarin platform, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available. E-mail validation is called and an E-mail has a format, which is like [email protected]. Thus, this article describes how to validate an E-mail.

Prerequisites

  • Visual Studio 2015 Update 3.

The steps, given below are required to be followed in order to validate an E-mail address in Xamarin Android app, using Visual Studio 2015.

Step 1


Click File--> select New--> select Project. The project needs to be clicked after opening all the types of the projects in Visual Studio or clicking (Ctrl+Shift+N).

select Project

Step 2

After opening the New Project, select Installed-->Templates-->Visual C#-->Android-->choose the Blank app (Android).

Now, give your Android app a name (Ex:sample) and give the path of your project. Afterwards, click OK.

select Project

Step 3

Now, go to Solution Explorer. In Solution Explorer, get all the files and sources in your project.

Now, select Resource-->Layout-->double click to open main.axml page. To write XAML code, you need to select the source .

Choose the Designer Window if you want to design it and you can design your app.

select Project

Step 4

After opening main.axml, file will open the main page designer. You can design the page, as per your desire.

select Project

Now, delete Default hello world button. Go to the source panel and you can see the button coding. You need to delete it.

After deleting the XAML code, delete C# button action code.

Go to the MainActivity.cs page. You need to delete the button code.

Step 5

Now, go to the toolbox Window. In the toolbox Window, get all the types of the tools and controls.

You need to go to the toolbox Window. Now, scroll down and you will see all the tools and controls.

You need to drag and drop the Plain Text (EditText).

select Project

Step 6

You need to drag and drop the TextView, as shown below.

select Project

Step 7


You need to drag and drop the Button, as shown below.

select Project

Step 8

Now, go to the properties Window. You need to edit the Plain Text (EditText) Id Value (EX: android:id="@+id/txtemail").

select Project

Step 9

You need to edit the TextView Id Value (EX: android:id="@+id/txtdisplay" ).

select Project

Step 10

Also, edit the Button's Id value and Text value. (Ex: android:id="@+id/btnvalidate" android:text="Validate")

select Project

Step 11

In this step, go to the Main.axml page Source Panel. Note, the EditText's Id value, TextView's Id value and Button's Id Value.

Main.axml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">  
  2.     <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/txtemail" android:hint="[email protected]" />  
  3.     <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/txtdisplay" />  
  4.     <Button android:text="Validate" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btnvalidate" /> </LinearLayout>// This is just a sample script. Paste your real code (javascript or HTML) here. if ('this_is'==/an_example/){of_beautifier();}else{var a=b?(c%d):e[f];}  
select Project

Step 12

In this step, create a method, which is called isValidEmail in MainActivity.cs page.

MainActivity.cs
  1. public bool isValidEmail(string email) {  
  2.     return Android.Util.Patterns.EmailAddress.Matcher(email).Matches();  
  3. }  
select Project

Step 13

In this step, go to the MainActivity.cs Page in Solution Explorer. Write the code, mentioned below Between OnCreate() Method.

MainActivity.cs
  1. protected override void OnCreate(Bundle bundle) {  
  2.     base.OnCreate(bundle);  
  3.     SetContentView(Resource.Layout.Main);  
  4.     Button submit = FindViewById < Button > (Resource.Id.btnvalidate);  
  5.     submit.Click += delegate {  
  6.         EditText email = FindViewById < EditText > (Resource.Id.txtemail);  
  7.         string inputemail = email.Text.ToString();  
  8.         TextView textdisplay = FindViewById < TextView > (Resource.Id.txtdisplay);  
  9.         var emailvalidate = isValidEmail(inputemail);  
  10.         if (inputemail == "") {  
  11.             textdisplay.Text = "Enter the Email.!";  
  12.         } else {  
  13.             if (emailvalidate == true) {  
  14.                 textdisplay.Text = "Email is Valid";  
  15.             } else {  
  16.                 textdisplay.Text = "Email is Not Valid";  
  17.             }  
  18.         }  
  19.     };  
  20. }  
select Project

Step 14

If you have an Android Virtual device, run the app on it, else connect your Android phone and run the app on it.

Simply, connect your phone and go to Visual Studio. The connected phone will show up in the Run menu (Ex:LENOVO A6020a40(Android 5.1-API 22)). Click Run option.

select Project

Output

After a few seconds, the app will start running on your phone.

You will see your app run successfully.

select Project

Now, you can give any value, and click Validate Button. It shows the Error Value. "Email is Not value".

select Project

select Project

You need to give the correct format of the email Address. You will see the text value, which is showing "Email is Valid".

select Project

Summary

Hence, this was the process of how to validate an E-mail Address in Xamarin Android app, using Visual Studio 2015.

Up Next
    Ebook Download
    View all
    Learn
    View all