How could I do an if statment such as:
Understand?Code:if pnum.contains 3 "-" then
msgbox "contains three dashes!"
else
msgbox "n/a"
end if
Printable View
How could I do an if statment such as:
Understand?Code:if pnum.contains 3 "-" then
msgbox "contains three dashes!"
else
msgbox "n/a"
end if
Split the string by the delimiter ("-") and count the length of the array.
There may be a faster way.
Cue faster way...
Quote:
Originally Posted by mendhak
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
Or a statment like.
If str contains 13 numbers than
This would would be best...
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.
I simply need to know if the str contains no more and no less than 13 numbers.
Yes, but why?
Are you trying to validate a phone number, for example?
yes.Quote:
Originally Posted by mendhak
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
Use a regular expression.
http://regexlib.com/DisplayPatterns....&cattabindex=2
You can use one or make your own.
Did I mention that you needed to be clear? This:Quote:
Originally Posted by thud
1x2x3x4x5x6x7x8x9x0x1x2x3
would pass your test but it's hardly a valid phone number, is it?
thanks schnookumz
+ rep for you.
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
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.
MORE RANTING
What if the guy's British?
+(011) 44 2088784765
Or French?
011 33 144543171
My point exactly. I can't believe I agree with TS:eek:
I know. I'm frightened too.Quote:
Originally Posted by dbasnett
it is frightening that you agree with yourself. i crack me up sometimes;)
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.Quote:
Originally Posted by CoachBarker
That's way simpler than trying to validate a free form box.
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.
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...Quote:
Originally Posted by CoachBarker
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.
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 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
Code:Public Function GetNumbersInString(ByVal text As String) As String
Return System.Text.RegularExpressions.Regex.Replace(text, "\D+", String.Empty)
End Function