|
-
Mar 26th, 2008, 03:14 AM
#1
Thread Starter
Hyperactive Member
[2008] If string has three dashes then....
How could I do an if statment such as:
Code:
if pnum.contains 3 "-" then
msgbox "contains three dashes!"
else
msgbox "n/a"
end if
Understand?
-
Mar 26th, 2008, 03:19 AM
#2
Re: [2008] If string has three dashes then....
Split the string by the delimiter ("-") and count the length of the array.
There may be a faster way.
Cue faster way...
-
Mar 26th, 2008, 03:22 AM
#3
Thread Starter
Hyperactive Member
Re: [2008] If string has three dashes then....
 Originally Posted by mendhak
Split the string by the delimiter ("-") and count the length of the array.
There may be a faster way.
Cue faster way...
Or how about a statement for if a string contains any letters?
If str.contains letters then
msgbox "contains numbers"
else
msgbox "contains letters"
end if
-
Mar 26th, 2008, 03:23 AM
#4
Thread Starter
Hyperactive Member
Re: [2008] If string has three dashes then....
Or a statment like.
If str contains 13 numbers than
This would would be best...
-
Mar 26th, 2008, 03:35 AM
#5
Re: [2008] If string has three dashes then....
How about you tell us clearly what it is you want to examine this string for, because everything you've posted so far is different. If we don't know what you want then we can't tell you how to get it. BE CLEAR! For instance, would nay string with 13 digits do, even if it had 5000 more digits and 10000 letters in it too? You see what I'm saying? You need to be SPECIFIC about what is and isn't valid.
-
Mar 26th, 2008, 03:40 AM
#6
Thread Starter
Hyperactive Member
Re: [2008] If string has three dashes then....
I simply need to know if the str contains no more and no less than 13 numbers.
-
Mar 26th, 2008, 03:43 AM
#7
Re: [2008] If string has three dashes then....
Yes, but why?
Are you trying to validate a phone number, for example?
-
Mar 26th, 2008, 03:48 AM
#8
Thread Starter
Hyperactive Member
Re: [2008] If string has three dashes then....
 Originally Posted by mendhak
Yes, but why?
Are you trying to validate a phone number, for example?
yes.
-
Mar 26th, 2008, 03:51 AM
#9
Fanatic Member
Re: [2008] If string has three dashes then....
why don't you split the string on space (" ") which is the end of word and then check the each word to see if it is a number.
vb Code:
dim words() as String = sentence.split(new Char() {" "c}) for each str as String in words if isNumeric(str) then counter += 1 if counter = 13 then exit for next str
-
Mar 26th, 2008, 03:53 AM
#10
Re: [2008] If string has three dashes then....
Use a regular expression.
http://regexlib.com/DisplayPatterns....&cattabindex=2
You can use one or make your own.
-
Mar 26th, 2008, 03:54 AM
#11
Re: [2008] If string has three dashes then....
 Originally Posted by thud
yes.
Did I mention that you needed to be clear? This:
1x2x3x4x5x6x7x8x9x0x1x2x3
would pass your test but it's hardly a valid phone number, is it?
-
Mar 26th, 2008, 03:56 AM
#12
Hyperactive Member
Re: [2008] If string has three dashes then....
thanks schnookumz
+ rep for you.
-
Mar 26th, 2008, 08:15 AM
#13
Frenzied Member
Re: [2008] If string has three dashes then....
This is how we checked for a valid phone number in a number of school projects. We stored it in a module for utility functions.
Code:
' Method name: isPhone
' Purpose: determine if a string is a valid phone number
' Parameters: a string representing a phone number
' Return: boolean indicating validity
Public Function isPhone(ByVal PhoneNumber As String) As Boolean
Try
' Phone #'s are expected in the format (###) ###-####
If PhoneNumber.Length = 14 Then
If PhoneNumber.StartsWith("(") And _
IsNumeric(PhoneNumber.Substring(1, 3)) And _
PhoneNumber.Substring(4, 2) = ") " And _
IsNumeric(PhoneNumber.Substring(6, 3)) And _
PhoneNumber.Substring(9, 1) = "-" And _
IsNumeric(PhoneNumber.Substring(10, 4)) Then
Return True
End If
End If
Return False
Catch ex As Exception
MsgBox("An error occured in mdlUtilities. Method: isPhone. Error: " & ex.Message)
Return False
End Try
End Function
-
Mar 26th, 2008, 08:38 AM
#14
Re: [2008] If string has three dashes then....
RANTING
Phone number validation?
3146354494
314-635-4494
314.635.4494
(314) 635-4494
and what about extensions.
314-635-4494 ext 1234
314-635-4494 x 1234
MY OPINION is that phone numbers should be free form text, unless the data is to be used by some machine i.e. autodialer. At most I only verify that there are some minimum number of digits.
If I have a format I prefer I might display the number as such.
-
Mar 26th, 2008, 08:47 AM
#15
Re: [2008] If string has three dashes then....
MORE RANTING
What if the guy's British?
+(011) 44 2088784765
(VB/C#) is clearly superior to (C#/VB) because it (has/doesn't have) <insert trivial difference here>.
-
Mar 26th, 2008, 08:51 AM
#16
Fanatic Member
Re: [2008] If string has three dashes then....
Or French?
011 33 144543171
CLanguage; 
IF Post = HelpFull Then
RateMe
Else
Say("Shut UP")
End If
DotNet rocks
VB 6, VB.Net 2003, 2005, 2008, 2010, SQL 2005, WM 5.0,ahem ?OpenRoad?
-
Mar 26th, 2008, 08:52 AM
#17
Re: [2008] If string has three dashes then....
My point exactly. I can't believe I agree with TS
-
Mar 26th, 2008, 09:09 AM
#18
Re: [2008] If string has three dashes then....
 Originally Posted by dbasnett
My point exactly. I can't believe I agree with TS 
I know. I'm frightened too.
(VB/C#) is clearly superior to (C#/VB) because it (has/doesn't have) <insert trivial difference here>.
-
Mar 26th, 2008, 09:17 AM
#19
Re: [2008] If string has three dashes then....
it is frightening that you agree with yourself. i crack me up sometimes
-
Mar 26th, 2008, 09:24 AM
#20
Frenzied Member
Re: [2008] If string has three dashes then....
It was a criteria of the projects, that we provided a function to validate various data entries. The phone number was required to be in that format. It could be changed to work for any format or language. Basically I was only providing an example of how we did it.
-
Mar 26th, 2008, 09:36 AM
#21
Re: [2008] If string has three dashes then....
 Originally Posted by CoachBarker
It was a criteria of the projects, that we provided a function to validate various data entries. The phone number was required to be in that format. It could be changed to work for any format or language. Basically I was only providing an example of how we did it.
In that case, your simplest method would be to restrict the user's input. Instead of having a phone number text box, have three text boxes - one for area code and one for the next three digits and one for the last four. Make them all require numeric entries and give them a max length of 3, 3, 4. Then concatenate those three boxes together and add the dashes in order to get the phone number.
That's way simpler than trying to validate a free form box.
(VB/C#) is clearly superior to (C#/VB) because it (has/doesn't have) <insert trivial difference here>.
-
Mar 26th, 2008, 10:15 AM
#22
Re: [2008] If string has three dashes then....
Hey coach, I didn't mean to sound as if I was picking on you, I guessed that it was a 'requirement'. My problem is with the people that have those requirements. My experience has been that they are typically personal likes / dislikes, but if it is your dime have a blast.
Can you imagine the discussions about DNS and names? There was actually thought of restricting labels to 24 bytes and storing DNS records in binary. Some would say that the DNS requirements don't meet the needs of today.
-
Mar 26th, 2008, 10:31 AM
#23
Re: [2008] If string has three dashes then....
 Originally Posted by CoachBarker
This is how we checked for a valid phone number in a number of school projects. We stored it in a module for utility functions.
Code:
' Method name: isPhone
' Purpose: determine if a string is a valid phone number
' Parameters: a string representing a phone number
' Return: boolean indicating validity
Public Function isPhone(ByVal PhoneNumber As String) As Boolean
Try
' Phone #'s are expected in the format (###) ###-####
If PhoneNumber.Length = 14 Then
If PhoneNumber.StartsWith("(") And _
IsNumeric(PhoneNumber.Substring(1, 3)) And _
PhoneNumber.Substring(4, 2) = ") " And _
IsNumeric(PhoneNumber.Substring(6, 3)) And _
PhoneNumber.Substring(9, 1) = "-" And _
IsNumeric(PhoneNumber.Substring(10, 4)) Then
Return True
End If
End If
Return False
Catch ex As Exception
MsgBox("An error occured in mdlUtilities. Method: isPhone. Error: " & ex.Message)
Return False
End Try
End Function
Coach, just a heads up, that function can produce horribly wrong results, so if you are using it in all school projects, then all those projects have a bug...
Try passing a string like this to your function
Dim myString As String = "($15) $12-10.2"
and you will see it passes without a problem
I have said it over and over again on this forum, IsNumeric is NOT a good way to validate numeric. It will return true when a dollar sign is present followed by at least one number. It will also return true if its a valid decimal number. So you see someone could put in weird strings like the one I show above, and your code would think it was a perfectly valid phone number.
Here is a better method. It simply uses regex to get all numbers in a given string, and returns true if there are 10.
Code:
Public Function isPhone(ByVal PhoneNumber As String) As Boolean
Try
'GET JUST THE NUMBERS VIA REGEX (IE STRIP ANY AND ALL FORMATTING)
'AND RETURN TRUE IF THERE ARE 10 DIGITS, FALSE IF THERE ARE NOT
Return System.Text.RegularExpressions.Regex.Replace(PhoneNumber, _
"\D+", _
String.Empty).Length = 10
Catch ex As Exception
MsgBox("An error occured in mdlUtilities. Method: isPhone. Error: " & ex.Message)
Return False
End Try
End Function
Likewise, if you just wanted a function get the digits (as a string) so you could then format them accordingly, you can use the same regex method in its own function, like so:
Code:
Public Function GetNumbersInString(ByVal text As String) As String
Return System.Text.RegularExpressions.Regex.Replace(text, "\D+", String.Empty)
End Function
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|