EMail Validation in Windows Application

Santosh4u
Posted by Santosh4u under Windows Forms category on | Views : 12163
VB.NET
Imports System.Text.RegularExpressions
Imports System.Text.RegularExpressions.Regex
Public Class TabControl
Shared Function EmailValidation(ByVal ctxt As System.Windows.Forms.TextBox) As Boolean
If ctxt.Text.Trim <> "" Then
Dim rex As Match = Regex.Match(Trim(ctxt.Text), "^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+
[a-zA-Z]{2,3})$", RegexOptions.IgnoreCase)
If rex.Success = False Then
MessageBox.Show("Please Enter a valid Email-Address ", "Information", MessageBoxButtons.OK,
MessageBoxIcon.Information)
ctxt.Focus()
Return False
Else
Return True
End If
Else
Return True
End If
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
EmailValidation(TextBox1)
End Sub
End Class

C#.NET
 private void button1_Click(object sender, EventArgs e)
{
EmailValidation(textBox1);
}
public static bool EmailValidation(System.Windows.Forms.TextBox ctxt)
{
if (ctxt.Text.Trim() != "")
{
Match rex = Regex.Match(ctxt.Text.Trim(' '), "^([0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+
[a-zA-Z]{2,3})$", RegexOptions.IgnoreCase);
if (rex.Success == false)
{
MessageBox.Show("Please Enter a valid Email-Address ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
ctxt.Focus();
return false;
}
else
{
return true;
}
}
else
{
return true;
}
}

Regards
Santosh

Comments or Responses

Login to post response