connectseekers.com

Friday, 30 May 2014

Email Validation in Windows Application C#.Net and VB.Net

5/30/2014 - By Pranav Singh 1

Today email is have becomes most important part of our life. So whenever we are developing any application in either in windows or web ad asking user for email id of user, so before saving the data we must validate that email id entered by user must be valid.

So in this article I will show you how you can validate the email id in a windows application using C#.Net and VB.Net.


So for this article first we will create a windows application. Now add the use code.
C#.Net
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace ProjectDemo_Windows
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int status = ValidateEmailId(textBox1.Text);
            if (status == 0)
            {
                MessageBox.Show("E-Mail Id expected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (status == 1)
            {
                MessageBox.Show("Thanks for ptoviding a valid E-mail Id. ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (status == 2)
            {
                MessageBox.Show("Please enter E-mail Id", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        }
        /// <summary>
        /// This method is used for validating email if provied by the user
        /// </summary>
        /// <param name="emailId"></param>
        /// <returns>0 - Invalid Eail id</returns>
        /// <returns>1 - Valid Email id</returns>
        /// <returns>2 - Not data found</returns>
        public int ValidateEmailId(string emailId)
        {
            /*Regular Expressions for email id*/
            System.Text.RegularExpressions.Regex rEMail = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
            if (emailId.Length > 0)
            {
                if (!rEMail.IsMatch(emailId))
                {
                    return 0;
                }
                else
                {
                    return 1;
                }
            }
            return 2;
        }
    }
}


In above code we have user public int ValidateEmailId(string emailId) function to validate the email id.

VB.Net
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.Collections

Namespace ProjectDemo_Windows
    Partial Public Class Form1
        Inherits Form
        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim status As Integer = ValidateEmailId(textBox1.Text)
            If status = 0 Then
                MessageBox.Show("E-Mail Id expected", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
            ElseIf status = 1 Then
                MessageBox.Show("Thanks for ptoviding a valid E-mail Id. ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
            ElseIf status = 2 Then
                MessageBox.Show("Please enter E-mail Id", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            End If

        End Sub
        ''' <summary>
        ''' This method is used for validating email if provied by the user
        ''' </summary>
        ''' <param name="emailId"></param>
        ''' <returns>0 - Invalid Eail id</returns>
        ''' <returns>1 - Valid Email id</returns>
        ''' <returns>2 - Not data found</returns>
        Public Function ValidateEmailId(ByVal emailId As String) As Integer
            'Regular Expressions for email id

            Dim rEMail As New System.Text.RegularExpressions.Regex("^[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$")
            If emailId.Length > 0 Then
                If Not rEMail.IsMatch(emailId) Then
                    Return 0
                Else
                    Return 1
                End If
            End If
            Return 2
        End Function
    End Class
End Namespace

Now run the application for final output



About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

1 comment:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top