Numeric String Cleaner Function

Why do this?

Your phone number (w/area code): () -

Your SSN: - -

Your CC number: - - -

Your Zip Code: -

When you can do this...

Your phone number (w/area code):

Your SSN:

Your CC number:

Your Zip Code:

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


 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.

Try me...

Enter 10 numbers, that looks like a 10-digit phone number (aaa) bbb-cccc - in any format you like:
* include ()./- (if you like...)

Enter 9 numbers, that looks like a social security number:
* include - (if you like...)

The "Try it" fields do not handle bad data - they are general examples only. For your code - you would need to handle conditions like - out-of-order numbers, extra numbers, or not enough numbers.

Use it already! THANK YOU!!!

Seen it before, but haven't used it - please do so now!

News to you, great... consider this "Shared Knowledge" and please fix your forms!


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 would 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?
White Pages
AT & T White Pages - found to be a one field entry Jan 2008. Thank YOU!!!

Examples of Multi-Entry Phone number field?
None right now...

Or others can be found via any search engine - search term: "Phone Lookup"