C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Validation for the entered e-mail in textbox


Posted Date: 19 Nov 2004    Resource Type: Articles    Category: .NET Framework
Author: Swapnil KumarMember Level: Silver    
Rating: 1 out of 5Points: 5



You can use the following code snippet to validate an email address.

Create a new windows application of vb.net and place a textbox named textbox1 and a button. on the click of that button paste the following code


ChkValidEmail(TextBox1.Text)

' Now paste the following code

Public Function ChkValidEmail(ByVal Value As String, Optional ByVal MaxLength As Integer = 255, Optional ByVal IsRequired As Boolean = True) As Boolean
If Value Is Nothing OrElse Value.Length = 0 Then
Return Not IsRequired
ElseIf Value.Length > MaxLength Then
Return False
End If
If Not System.Text.RegularExpressions.Regex.IsMatch(Value, "^[-A-Za-z0-9_@.]+$") Then
MsgBox(" Invalid character in the e-mail address. Only(A-Z,a-z,_,0-9,@,.)is valid. ")
TextBox1.Clear()
TextBox1.Focus()
Return False
End If
' search the @ char
Dim i As Integer = Value.IndexOf("@"c)
' there must be at least three chars after the @
If i <= 0 Or i >= Value.Length - 3 Then
MsgBox("INVALID E-Mail: '@' missing.")
TextBox1.Clear()
TextBox1.Focus()
Exit Function
End If
' ensure there is only one @ char
If Value.IndexOf("@"c, i + 1) >= 0 Then
MsgBox(" INVALID E-Mail: '@'Cann't be allowed twice.")
TextBox1.Clear()
TextBox1.Focus()
Exit Function
End If
' check that the domain portion contains at least one dot
Dim j As Integer = Value.LastIndexOf("."c)
' it can't be before or immediately after the @ char
If j < 0 Or j <= i + 1 Then
MsgBox(" INVALID E-Mail: '.' missing. ")
TextBox1.Clear()
TextBox1.Focus()
Exit Function
End If
' if we get here the address if validated
MsgBox(" Congratulations Operator:- Entered E-Mail Address is valid. ")
Return True
End Function



Summary

By this code u can easily validate the e-mail entered in the textbox




Responses

Author: Rajesh Panda    22 Nov 2004Member Level: Bronze   Points : 0
Dear Friends,
There is small bug in this program.That is after successful validation of the email address the address has been cleared.so just ignore the last 2 lines of the code.


Author: Babu Aboobacker    23 Mar 2005Member Level: Silver   Points : 0
I have got a simple Regex string from MSDN for validating email id.

private bool IsValidEmail(string strIn)
{
// Return true if strIn is in valid e-mail format.
return System.Text.RegularExpressions.Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); }


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Javascript Cookie handling
Previous Resource: NMock to help solve the problem of writing Unit Test code to your .NET module
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use