Jump to content

Testing string for char = "@"


Recommended Posts

I had to travel back home for a couple days on Friday and left my laptop here so couldn't do much VB learning at home and completely forgotten most of the stuff, but I'm hoping it will come back. :)

But I'm now trying to test the text which goes into a text box (TextBox2) to see if it is a valid email address (contains @).

I can do the variable naming, passing text to variables, and message box but I can't remember how to create the variable @ and then loop through the text.

I'm using the following code at the minute if it helps;

	Private Sub CheckEmail()

Dim email As String = Trim(TextBox2.Text)
Dim at As Char = "@"
Dim i As Integer

For i = 0 To TextBox2.Text
If at = "@" Then
MsgBox("Please enter a valid email address")
End If
Next i


End Sub
End Class

When I run it, the error that comes up says "When casting from a number, the value must be less than infinity"

I think it's the For Loops that are confusing me again because I didn't have enough time to learn to understand them properly. :(

EDIT:-

The variable "email" is the email address that is entered into TextBox2.

The variable "@" is the character that I'm trying to look for inside TextBox2.

Link to comment
Share on other sites

Done it this way actually, but I don't want the first message box to come up if email = at, which is the next bit of what I have to do. :(

	Private Sub CheckEmail()

Dim email As String = Trim(TextBox2.Text)
Dim at As Char

at = "@"

If email = at Then
MsgBox("Thank you for your email address")
Else
MsgBox("Please enter a valid email address")
End If

Couldn't see Edit button, so had to post a second time. :(

Link to comment
Share on other sites

Both these versions do absolutely nothing of any value - what were you thinking when you wrote these...?

What you want to do is check a string - each position - if it contains one at-mark. If it contains zero - or more than one, then it is wrong. Now look at this sample string

|a|b|c|d|e|@|d|o|m|a|i|n|.|n|e|t|

 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1

 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5

Here you can see that we have a string of 16 characters, so we want to check every position from 0 to 15. We can do that easiest with a For loop, again.

		For i = 0 to email.Length

Then you will need an additional integer as a counter.

			If email.chars(i) = "@" Then
counter = counter + 1

And after the loop completion you simply ask

		If counter = 1 Then
MsgBox("Thank you for your email address")
Else
MsgBox("Please enter a valid email address")
End If

It's as simple as that...

Link to comment
Share on other sites

Would you like to make this program a little bit more interesting (complex) ?

First, show me the program you have so far.

Next, expand the program in two steps

  1. test that there is at least one full stop '.' after the '@' sign.
  2. test that there are no illegal characters in the whole email address string.

Illegal characters are shown on this page (NO).

Link to comment
Share on other sites

Illegal characters being unicodes, I can't do them with this laptop keyboard but normally you would press alt + number, number, number, etc on the right hand side number pad?

 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Call CheckEmail()



End Sub





Private Sub CheckEmail()
Dim email As String
Dim counter As Integer
Dim i As Integer

email = Trim(TextBox2.Text)

For i = 0 To email.Length - 1
If email.Chars(i) = "@" Then
counter = counter + 1
End If
Next i

If counter = 1 Then
MsgBox("Thank you for your email address")
ElseIf counter > 1 Then
MsgBox("Enter a valid email addres")
ElseIf counter < 1 Then
MsgBox("Enter a valid email address")
End If
End Sub

Would the full stop be a char or string variable?

Link to comment
Share on other sites

Illegal characters are shown on this page (NO).

I'm an idiot; I forgot to paste the actual page: http://www.remote.org/jochen/mail/info/chars.html

Illegal characters being unicodes, I can't do them with this laptop keyboard but normally you would press alt + number, number, number, etc on the right hand side number pad?

Let's only deal with single byte characters here. Theoretically multibyte (Unicode) characters are valid in email addresses, but I don't think we should start to deal with wide characters at this early stage of your VB training.

		If counter = 1 Then
MsgBox("Thank you for your email address")
ElseIf counter > 1 Then
MsgBox("Enter a valid email addres")
ElseIf counter < 1 Then
MsgBox("Enter a valid email address")
End If

This additional checking for > 1 or < 1 is completely unnecessary; just use a simple Else here.

Would the full stop be a char or string variable?

Just a character constant, as you earlier used the '@'.

As for the invalid/illegal character checking, I think it would be easier to check for valid characters, as there are fewer than invalid ones. Check only the OK ones ('+', '-', '0'~'9', 'A'~'Z', '_', 'a'~'z'). You could place them all into a sting, using each of its bytes for testing. Sounds like fun! :P

Link to comment
Share on other sites

Your little example does not make much sense...

Remember the litte For loop we had earlier? It should be placed in there, and have each char in the string checked. For example

		For i = 0 To email.Length - 1
Select Case email.Chars(i)
Case '@'
atcount = atcount + 1
Case '.'
fullstop = fullstop + 1
Case '0' to '9', 'A' to 'Z', 'a' to 'z', '+', '-', '_'
REM - valid character, nothing to do.
Case Else
invalid = invalid + 1
End Select
Next i

This way you count the number of '@' characters as before (with a Case statement instead of If), and additionally you check for '.' and other valid characters, counting the invalid ones in the Case Else clause.

Outside of the For loop you will check for atcount = 1, fullstop >= 1, and invalid = 0 – otherwise the email address is invalid.

Link to comment
Share on other sites

Here's what I had to do for an assignment at Tafe:

<script>
var emailchecker=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i

function funcheckemail(form)
{

var objtxtinput = document.getElementById("emailtxt");

var vemail=objtxtinput.value

if (!emailchecker.test(vemail))
{
alert("Sorry, "+vemail+" is a bad email");
}
else
{
alert(vemail+" is valid!");
}


}
</script>

This was done in JavaScript, but it should be easy enough to convert to VB.Net.

if (!emailchecker.test(vemail)) <-- This line uses RegEx (Regular Expressions), so you will need to lookup the NameSpace for RegEx, and import it.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy