Simple "function"...
Function CleanNumericString( _
ByVal StringToClean As String)
Dim ReturnValue As String = String.Empty
For Counter As Integer = 0 To StringToClean.Length-1
Select Case StringToClean.Substring(Counter, 1)
Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
ReturnValue &= StringToClean.Substring(Counter, 1)
Case Else
' skip the character
End Select
Next
Return ReturnValue
End Function
The above function looks at each character of a string (passed), if it is a number
(0 to 9), it continues to build a new string with it. If it is not a number, it
skips it and goes on to the next. When the loop reaches the end of the string, it
returns the cleaned string to you.
Yep - that's it!
This Visual Basic.NET code should be easy to understand & convert to any popular
language (C++, C#, Delphi, Turbo Pascal, PHP, etc.).
This is just a "for loop" and a "case statement" and a way to handle the string
- one character at a time. In VB.NET, this is the SubString method. In other languages,
you may have to create a character array.
It can be done -and- it is simple - so, start using it.
Developers...
This process is not hard to implement! Stop forcing people to fill in multiple fields
for common data (viz. phone, social security, etc). Stop using excess code to auto-jump
to the next field!
This kind of data is just a string of numbers and the code below simply removes
all non-number characters and returns an unformatted numeric string to you, which
you can then format in the manner that suits your needs.
Data can be entered in any form...
- (000) 555-1234
- 000-555-1234
- 000-000-0000
- 000 000 0000
- etc
It doesn't matter. As long as they enter the numbers in proper order - that is what
you will get! The extra characters just don't matter. No need to jump through extra
hoops.
Incorporate this type of function into your processes, now!
Stop working so hard... Stop writing unnecessary code...
Hint: if you want... you could also use "Regular Expressions". Many languages make
this available. Study up if you like...
Where did this come from?
Back in the "old days" when RAM or disk space was tight, programmers use to strip
data down to its smallest required parts. Getting rid of formatting characters that
were either non-standard, unnecessary, or that just took up valuable space - was
common practice. As RAM & disk space became more available - new programmers never
learned this art of conservation, and instead implemented a terrible result!
That result? The all to common multi-field entry for common everyday numbers. Do
your part in eradicating them! Don't use them yourself, and when you see them in
use - point the developer to this page.
Examples of Single-Entry Phone number field?
Google
SBC White Pages
AT & T White Pages -
found to be a one field entry Jan 2008. Thank YOU!!!
Examples of Multi-Entry Phone number field?
Verizon FiOS
Or others can be found via any search engine - search term: "Phone Lookup"