about email validation

mmsteelers

Member
Joined
Feb 4, 2009
Messages
9
Programming Experience
Beginner
Hi I have to make a basic program that:
Create a program with a simple interface that
1. Asks the user type his/her email address into a textbox.
2. When the user clicks the "Evaluate Email Address" button, one of two message boxes pops up:
"That is a valid email address!" with an "Information" icon, or "That is an invalid email address!" and on a second line, "Please retype your email address.", with an "Exclamation" icon
3. To be considered valid, the email address must
-include an "@" symbol, and
-it must end in either ".com", ".net", or ".org" or ".edu"
(I don't know how to check a string and test if its last four characters end with any of these and make it say not valid email address. I think I might have to use a case statement)

I really need help on number 3. That is where I am really stuck.

I am pretty sure that this shouldn't use regular expressions. It doesn't have to be perfect. It just has to meet the requirements of number 3.

Sorry for the noob question. I am taking intro to visual basic right now. So I don't know too much about Vb.net. Thanks to anyone who can help me with this.
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
Use regex (regular expression) for that, I use this pattern
Code:
Friend Const g_RegexEmailPatternString As String = "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
 

mmsteelers

Member
Joined
Feb 4, 2009
Messages
9
Programming Experience
Beginner
I just found out that my teacher wants me to use indexof to verify that it has the @, .com, .org, .gov and .edu in the address. How do I do that?
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
What does MSDN tell you about the IndexOf function? The sample code shows you how to use it.

Also using IndexOf for email validation's one of the worst ways to validate it.
 

mmsteelers

Member
Joined
Feb 4, 2009
Messages
9
Programming Experience
Beginner
do you know how to search a string using a wildcard (*.com)? (to answer your question regex is much better but my teacher wants us to do it this way because we are beginners)
 

mmsteelers

Member
Joined
Feb 4, 2009
Messages
9
Programming Experience
Beginner
I uploaded my code to mediafire:
Email Address Validator 1.rar
This was another way I was thinking of validating an email:
Email Address Validator 2.rar

this is all of my code: (yes i know it looks really bad, as I am a noob):
I can't get it to work what should I do? Please help me fix this code, I am lost.

Public Class Form1

Private Sub btnValidate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnValidate.Click
Dim email As String = txtEmail.Text.Trim ' Email address
Dim isValid As Boolean ' To signal the input's validity

' Initialize the flag to True
isValid = True

If email.Length >= 6 Then
If Not txtEmail.Text = ("@") Then
isValid = False
End If
If email.EndsWith(".com") Then
ElseIf email.EndsWith(".edu") Then
ElseIf email.EndsWith(".net") Then
ElseIf email.EndsWith(".org") Then
Else
isValid = False
End If
Else
isValid = False
End If
' Display a message indicating that the email
' is valid or invalid.
If isValid Then
MessageBox.Show("Your email address is valid.")
Else
MessageBox.Show("Your email address is not valid.")
txtEmail.Focus()
txtEmail.SelectAll()
End If
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
'Clear everything
txtEmail.Clear()
End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
'Close App
Me.Close()

End Sub
End Class
 
Last edited:

MattP

Well-known member
Joined
Feb 29, 2008
Messages
1,206
Location
WY, USA
Programming Experience
5-10
It does look like you've given this a fair attempt so I'll try and give you a nudge in the right direction.

It looks like you're testing for 3 different criteria: Length, Contains '@", and EndsWith. I would suggest using nested-if statements where if the criteria is met you got to the next statement and if not you set the isValid to False.

Notice the .ToLower() method on setting your email variable. I'm assuming you want to count .COM, .Com, .cOm, etc as valid domains.

Code:
	Private Sub uxValidate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles uxValidate.Click

		Dim email As String = Me.uxEmailAddress.Text.Trim.ToLower()
		Dim isValid As Boolean = True

		If email.Length >= 6 Then
			If email.Contains("@") Then
				If email.EndsWith(".com") OrElse email.EndsWith(".edu") OrElse email.EndsWith(".net") OrElse email.EndsWith(".org") Then
					'Do Nothing
				Else
					isValid = False
				End If
			Else
				isValid = False
			End If
		Else
			isValid = False
		End If

		If isValid = True Then
			MessageBox.Show("Address Valid")
		Else
			MessageBox.Show("Address Invalid")
		End If

	End Sub
 

mmsteelers

Member
Joined
Feb 4, 2009
Messages
9
Programming Experience
Beginner
Thanks a lot. You really helped get me in the right direction. Your code was very helpful.
 
Top Bottom