CnB Posted June 7, 2011 Posted June 7, 2011 Hi everybody, i am new here and hoping to learn a thing or two. I am having 2 textboxes which require a telephone number and a postcode. The telephone one i got working, but the postcode is giving me a hard time. This is my code for the control of the telephone textbox. Public Function ControlTelephone(ByVal StrTelephone As String) As Boolean Dim BlnTestresult As Boolean BlnTestresult = True Dim IntTeller As Integer = 0 If Len(StrTelephone) = 10 Then For IntTeller = 1 To 3 If Microsoft.VisualBasic.Mid(StrTelephone, IntTeller, 1) < "0" Or Microsoft.VisualBasic.Mid _ (StrTelephone, IntTeller, 1) > "9" Then BlnTestresult = False End If Next If Microsoft.VisualBasic.Mid(StrTelephone, 4, 1) = "/" Then Else BlnTestresult = False End If For IntTeller = 5 To 10 If Microsoft.VisualBasic.Mid(StrTelephone, IntTeller, 1) < "0" Or Microsoft.VisualBasic.Mid _ (StrTelephone, IntTeller, 1) > "9" Then BlnTestresult = False End If Next Else BlnTestresult = False End If ControleTelefoon = BlnTestresult End Function I am trying to make a similar control for the postcode. The format should be 2 letters a space and 5 numbers like GE 64200 I got this code, but needles to say its not working like that. The main issue i am having is controlling the 2 letters. Public Function ControlePostcode(ByVal StrPostcode As String) As Boolean Dim BlnTestresult As Boolean BlnTestresult = True Dim IntTeller As Integer = 0 If Len(StrPostcode) = 7 Then For IntTeller = 1 To 2 If Microsoft.VisualBasic.Mid(StrPostcode, IntTeller, 1) < "A" Or Microsoft.VisualBasic.Mid _ (StrPostcode, IntTeller, 1) > "9" Then BlnTestresult = False End If Next If Microsoft.VisualBasic.Mid(StrPostcode, 3, 1) = " " Then Else BlnTestresult = False End If For IntTeller = 4 To 7 If Microsoft.VisualBasic.Mid(StrPostcode, IntTeller, 1) < "0" Or Microsoft.VisualBasic.Mid _ (StrPostcode, IntTeller, 1) > "9" Then BlnTestresult = False End If Next Else BlnTestresult = False End If ControlePostcode = BlnTestresult End Function I am hoping someone could help me out get the one for the postcode to work. P.S there is of course a code on the form which calls this class. Private Sub TxtTelephone_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtTelephone.LostFocus Dim Tel As New controle If Tel.ControleTelephone(TxtTelephone.Text) Then Else MsgBox("Wrong") TxtTelephone.Text = "" End If Tel = Nothing End Sub Thanks Quote
Leaders snarfblam Posted June 7, 2011 Leaders Posted June 7, 2011 (edited) First I want to give you a few tips. When dealing with characters, you should place the letter "c" after a literal value. For example, the digit zero would be written as "0"c. The reason I recommend this is (A) it makes it clear to others (and yourself later on) that you are working with characters, and (B) there are some differences in the way VB deals with string values and character values. In this case I don't see a problem, but if your code more accurately conveys what you want to do, you have a smaller chance of running into a problem. Another tip is rather than typing out "Microsoft.VisualBasic.Mid" you can just use the "Substring" method of the string objects. I think this makes things easier to read. It's also more "standard". Microsoft.VisualBasic.Mid(StrTelephone, IntTeller, 1) ' becomes StrTelephone.Substring(IntTeller, 1) But since we're dealing with single characters, there's an even better way to access the string. You can treat it like an array of characters. To get the third character in the string, you could type "StrTelephone(2)". Also, you can use the handy functions of the Char class to check the characters. For example, Char.IsDigit would come in handy here. For IntTeller = 1 To 3 If Microsoft.VisualBasic.Mid(StrTelephone, IntTeller, 1) < "0" Or Microsoft.VisualBasic.Mid _ (StrTelephone, IntTeller, 1) > "9" Then BlnTestresult = False End If Next ' becomes For IntTeller = 1 To 3 If Char.IsDigit(StrTelephone(IntTeller)) Then BlnTestresult = False End If Next Now, on to your question of how to validate the post codes. It looks like there is an error in your code. You're checking if a character is between "A" and "9", when you probably mean to check if it's between "A" and "Z". An even better way to check the character would be to use the Char.IsLetter function. Edited June 7, 2011 by snarfblam Quote [sIGPIC]e[/sIGPIC]
CnB Posted June 7, 2011 Author Posted June 7, 2011 Thanks for the pointers i am going to try to use those tips. As for my control yes i did make a mistake with "A" "9" was supposed to be A / Z. The thing i am not completely understanding how to use the Char.isletter function. I am guessing the part of the numbers is working, but the 2 letters is not. If i use Char.IsLetter i should also use Char.IsDigit i guess. Quote
Leaders snarfblam Posted June 8, 2011 Leaders Posted June 8, 2011 The thing i am not completely understanding how to use the Char.isletter function. You would use it exactly the same way I used Char.IsDigit in my previous post. Just use Char.IsLetter for the characters you need to be a letter, and Char.IsDigit for the characters you need to be a digit (but read on). If i use Char.IsLetter i should also use Char.IsDigit i guess. Well... you tell me. For IntTeller = 1 To 3 If StrTelephone[intTeller] < "0" Or StrTelephone[intTeller] > "9" Then BlnTestresult = False End If Next or For IntTeller = 1 To 3 If Char.IsDigit(StrTelephone[intTeller]) Then BlnTestresult = False End If Next Which one do you like better? They should both work, as far as I can tell, so it's really a matter of taste. Testing for letters is more complicated. If you use Char.IsLetter, the user will be allowed to enter things like "Њθ 90210". Char.IsLetter returns true for letters in any script, not just Latin letters. So it might actually be a better idea to manually check if the letters are in the range A-Z. You also have to consider whether you want to accept upper-case, lower-case, or both, and test accordingly. You might have been on the right track the first time. If I'm making this confusing, it's not intentional. Quote [sIGPIC]e[/sIGPIC]
CnB Posted June 9, 2011 Author Posted June 9, 2011 Not confusing at all, to be honest Char.IsDigit i find better. I got to try this out a bit see if i am getting this right. Seems easier that way. Thanks Quote
CnB Posted June 9, 2011 Author Posted June 9, 2011 You would use it exactly the same way I used Char.IsDigit in my previous post. Just use Char.IsLetter for the characters you need to be a letter, and Char.IsDigit for the characters you need to be a digit (but read on). Well... you tell me. For IntTeller = 1 To 3 If StrTelephone[intTeller] < "0" Or StrTelephone[intTeller] > "9" Then BlnTestresult = False End If Next or For IntTeller = 1 To 3 If Char.IsDigit(StrTelephone[intTeller]) Then BlnTestresult = False End If Next Which one do you like better? They should both work, as far as I can tell, so it's really a matter of taste. Testing for letters is more complicated. If you use Char.IsLetter, the user will be allowed to enter things like "Њθ 90210". Char.IsLetter returns true for letters in any script, not just Latin letters. So it might actually be a better idea to manually check if the letters are in the range A-Z. You also have to consider whether you want to accept upper-case, lower-case, or both, and test accordingly. You might have been on the right track the first time. If I'm making this confusing, it's not intentional. I have tested it a bit, but have not being able to get it to run yet. This is the code i got atm for my postcode Dim BlnTestresult As Boolean BlnTestresult = True Dim IntTeller As Integer = 0 If Len(StrPostcode) = 8 Then For IntTeller = 1 To 2 If Char.IsLetter(StrPostcode(IntTeller)) Then BlnTestresult = False End If Next ''' Code for space between 2 letters and 5 digits Else BlnTestresult = False End If For IntTeller = 4 To 8 If Char.IsDigit(StrPostcode(IntTeller)) Then BlnTestresult = False End If Next Else BlnTestresultaat = False End If ControlPostcode = BlnTestresult The part for the space between FL 33101for example, i dont get yet. Since i did not find a way to code that, unless using Microsoft.VisualBasic.Mid... The things i tried did not seem to work. Does this look right to you, since i am not used to this method yet. And i am not sure if i am doing it right. Any other solutions for coding the control for the space? Quote
Leaders snarfblam Posted June 10, 2011 Leaders Posted June 10, 2011 I'm very sorry. For two things actually. I couldn't approve your post yesterday because I was having trouble accessing the site (like-wise, I got your PM by e-mail but could not respond for the same reason). The other thing is that I haven't really been as helpful as I intended. I'd recommend you forget about Char.IsLetter (Char.IsDigit should work just fine, if you want to use it). The code I gave you also contained an error. I think this should pretty much do what you need. Dim BlnTestresult As Boolean = True Dim IntTeller As Integer = 0 [color="Green"]' Require 8 characters[/color] If Len(StrPostcode) = 8 Then [color="Green"] ' Require two letters[/color] For IntTeller = [color="Red"]0[/color] To [color="Red"]1[/color] If StrPostcode(IntTeller) < "A"[color="Red"]c[/color] Or StrPostcode(IntTeller) > "Z"[color="Red"]c[/color] Then BlnTestresult = False End If Next [color="Green"]' Require one space[/color] [color="Red"]If StrPostcode(2) <> " "c Then BlnTestresult = False End If[/color] [color="Green"]' Require five digits[/color] For IntTeller = [color="Red"]3[/color] To [color="Red"]7[/color] If [color="Red"]Not [/color]Char.IsDigit(StrPostcode(IntTeller)) Then BlnTestresult = False End If Next Else BlnTestresult = False End If ControlPostcode = BlnTestresult Note that the character indexes start at 0 (letters are chars #0 and #1, space is char #2, digits are chars #3-7). The reason I didn't use Char.IsLetter is because we only want Latin letters, whereas Char.IsLetter accepts letters from any script (Latin, Greek, Cyrillic, Hebrew, etc.). Note the "Not" used with Char.IsDigit. We want to set BltTestresult to false if the character is not a digit. Hope this helps. Quote [sIGPIC]e[/sIGPIC]
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.