Hb_Kai Posted December 9, 2008 Report Share Posted December 9, 2008 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 SubEnd ClassWhen 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. Quote Link to comment Share on other sites More sharing options...
Hb_Kai Posted December 9, 2008 Author Report Share Posted December 9, 2008 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 IfCouldn't see Edit button, so had to post a second time. :( Quote Link to comment Share on other sites More sharing options...
ɹəuəllıʍ ʇɐb Posted December 9, 2008 Report Share Posted December 9, 2008 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 5Here 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.LengthThen you will need an additional integer as a counter. If email.chars(i) = "@" Then counter = counter + 1And 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 IfIt's as simple as that... Quote Link to comment Share on other sites More sharing options...
Hb_Kai Posted December 9, 2008 Author Report Share Posted December 9, 2008 Thanks.That was a bit stupid actually. :( I could see where I was going wrong, but didn't think of using another variable to count the @ symbols. Quote Link to comment Share on other sites More sharing options...
ɹəuəllıʍ ʇɐb Posted December 10, 2008 Report Share Posted December 10, 2008 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 stepstest that there is at least one full stop '.' after the '@' sign.test that there are no illegal characters in the whole email address string.Illegal characters are shown on this page (NO). Quote Link to comment Share on other sites More sharing options...
Hb_Kai Posted December 10, 2008 Author Report Share Posted December 10, 2008 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 SubWould the full stop be a char or string variable? Quote Link to comment Share on other sites More sharing options...
ɹəuəllıʍ ʇɐb Posted December 11, 2008 Report Share Posted December 11, 2008 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.htmlIllegal 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 IfThis 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 Quote Link to comment Share on other sites More sharing options...
Hb_Kai Posted December 15, 2008 Author Report Share Posted December 15, 2008 If I'm testing for an array of characters, wouldn't it be best to use Select Case statements? Quote Link to comment Share on other sites More sharing options...
ɹəuəllıʍ ʇɐb Posted December 16, 2008 Report Share Posted December 16, 2008 Yes, exactly - this is the perfect case for a Select Case.Note that you can have case ranges, e.g. '0' to '9' etc. Quote Link to comment Share on other sites More sharing options...
Hb_Kai Posted December 16, 2008 Author Report Share Posted December 16, 2008 Select Case Error Case "character" Error = "A " " was found" Case "another character" DietState = "a " " was found"End SelectI know that's the syntax, but, is it actually necessary to list all of the characters in the Select Case statement? Quote Link to comment Share on other sites More sharing options...
ɹəuəllıʍ ʇɐb Posted December 16, 2008 Report Share Posted December 16, 2008 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 iThis 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. Quote Link to comment Share on other sites More sharing options...
Hb_Kai Posted December 16, 2008 Author Report Share Posted December 16, 2008 It looks like it'll work. :) But what's the REM - valid character, nothing to do line? Quote Link to comment Share on other sites More sharing options...
ɹəuəllıʍ ʇɐb Posted December 17, 2008 Report Share Posted December 17, 2008 REM = comment. Quote Link to comment Share on other sites More sharing options...
Slyke Posted December 18, 2008 Report Share Posted December 18, 2008 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+)$/ifunction 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.