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.
In my previous article I have shown How
to get the selected date of a MonthCalendar control in C#, Displaying
More than One Month in the Windows Forms MonthCalendar Control Using C#.Net,
How
to Open Command Prompt Using C#.net, How
to show Error & Warning Message Box in .NET | C# MessageBox.Show Examples.
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
it's good
ReplyDeleteJust wanna say that you have a very nice web site. I like the layout too, it actually stands out.
ReplyDeleteEmail Validation Service