Search Tools Links Login

Strip Non-Numeric Characters in ASP/VBScript


I have been asked for this little snippet of code by several users, so here it is. It does something that sounds really simple, but "classic" development languages from Microsoft don't seem to have a built in function for it.

Let's say that you are righting some sort of application that requires certain fields to contain numbers only. In some languages, such as VB6, you can limit the entry of characters to numbers directly at the input point, ie, the text box.

However, ASP/VBScript has not got that functionality, so you need to process that string with code, usually in your form validation routines. You do have a form validation routine, right?

Here is the code to perform the function, and I will step you through it below.

Function stripNonNumeric(stringIn)
  if stringIn <> "" then
    dim stringOut
    strIn = replace(stringIn,"'","")
    For I = 1 to len(stringIn)
      if isNumeric(mid(stringIn,I,1)) = true then stringOut = stringOut & mid(stringIn,I,1)
    Next
    stripNonNumeric = stringOut
  else
    stripNonNumeric = stringIn
  end if
  stripNonNumeric=Replace(StripNonNumeric,".","")
End Function

How It Works

I told you it was pretty straightforward, as far string processing goes. However, it confounds some of our developer-type counterparts.

There it is. Simply add this function to your string processing module, and you can reference it from anywhere.

About this post

Posted: 2013-08-25
By: vb6boy
Viewed: 9,417 times

Categories

Tutorials

Visual Basic Script (VBS)

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.