Email Validation In Xamarin iOS

Introduction

Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows phone, Android and an iOS). In Xamarin platform, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available.

Download code here.

 

Prerequisites
  • Xamarin Studio.
  • Xcode.
The steps given below are required to be followed in order to validate an Email address in Xamarin iOS, using Xamarin Studio.

Step 1

Go to Xamarin Studio.

Click New Solution—> select Multiplatform—> Choose Single View app. Native(iOS, Android). Afterwards, click Next.

 

Step 2

In this step, configure your app. Give app name (Ex:sample) and Organization Identifier and Choose Target Platforms. Now, choose Shared code. Afterwards, click Next.

 

Step 3

In this step, give your Project Name(Ex: Sample) and Solution Name(EX: Sample). Give the path of your project. Afterwards, click Create.

 

Step 4

Subsequently, go to Solution . In Solution, get all the files and source in your project. Now, select Main.storyboard and double click to open Main.storyboard page.

 

Step 5

After opening Main.storyboard, you can design the page, as per your desire.

 

Step 6

Now, go to Toolbox Window. In Toolbox Window, get all the types of the tools and controls you need to go to Toolbox Window.

Now, scroll down and you will see all the tools and controls. You need to drag and drop Text Field. Now, you need to align Text Field, using constraints.

 

Step 7

Now, go to the Properties Window. Select Text Field and give Name (Ex:txtEmail).

 

Step 8

You need to drag and drop the label.

Now, you need to align the label, using the constraints.

 

Step 9

Now, go to Properties Window. Select Label and give the name (Ex:lblMessage).

 

Step 10

You need to drag and drop the button.

Now, you need to align the button, using constraints.

 

Step 11

Now, go to Properties Window. Select the button and give the name (Ex:btnValidate).

 

Step 12

In this step, go to ViewController.cs page. Write the code given below.

ViewController.cs
  1. using System;  
  2. using System.Text.RegularExpressions;  
  3. using UIKit;  
  4. namespace XamariniOSEmailValidation {  
  5.     public partial class ViewController: UIViewController {  
  6.         protected ViewController(IntPtr handle): base(handle) {  
  7.             // Note: this .ctor should not contain any initialization logic.  
  8.         }  
  9.         public override void ViewDidLoad() {  
  10.             base.ViewDidLoad();  
  11.             // Perform any additional setup after loading the view, typically from a nib.  
  12.         }  
  13.         partial void BtnValidate_TouchUpInside(UIButton sender) {  
  14.             string email = txtEmail.Text;  
  15.             if (Regex.Match(email, @ "^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$").Success) {  
  16.                 lblMessage.Text = "Your Email (" + email + ") is Correct ";  
  17.                 lblMessage.TextColor = UIColor.Green;  
  18.             } else {  
  19.                 lblMessage.Text = "Your Email (" + email + ") is Incorrect ";  
  20.                 lblMessage.TextColor = UIColor.Red;  
  21.             }  
  22.         }  
  23.         public override void DidReceiveMemoryWarning() {  
  24.             base.DidReceiveMemoryWarning();  
  25.             // Release any cached data, images, etc that aren't in use.  
  26.         }  
  27.     }  
  28. }  


Step 13

Now, go to Run option. Choose Debug and the List of iPhone, available iPad Simulators. You can choose any one and run it.

 

Output

After a few seconds, the app will start running on your iPhone Simulator. You will see your app works successfully.

 

If you give the wrong format Email address, it will display, as shown below. 

 

If you give correct format Email address, it will display, as shown below.

 

Summary

This was the process of how to validate an Email address in Xamarin iOS, using Xamarin Studio.