fortypoundhead.com

Strip Non-Numeric Characters in ASP/VBScript

Posted On 2013-08-25 by VB6Boy
Keywords:
Tags: VBScript Visual Basic 6 Tutorial 
Views: 2240


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.

  • A check is made to ensure a value was passed. If no value was passed, then no work is done, and the same value is returned.
  • Apostrophes are removed from the string with the replace function. Some written (human) languages utilize apostrophes for separators in numbers similar to the way commas work in the english language. Since we only want numerics, and the apostrophe is not seen as an alpha character, we need to get rid of it from our string.
  • We iterate through the string, one character at a time, checking to see if the character is numeric. If it is, then we add it to the variable stringOut.
  • Finally at the end, we remove any periods from the string, normally used as a decimal indicator. This could've been removed in step 2 above, but I wanted to separate this out, just case there was a need to include a decimal point. The line can be simply commented out in the that case.

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

Questions

Drop me a message in the forums if you have any questions or comments regarding this code example. For more information on the Replace function, see this forum post.


About the Author

VB6Boy has posted a total of 7 articles.

 


Comments On This Post

No comments on this post yet!


Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.


Your IP address is:23.20.157.174

Before you can post, you need to prove you are human. If you log in, this test goes away.


Beat With A Stick List Returns: By request, BWASL has returned to the site. Free your rage by telling the world about something that really grinds your gears. Add your beating.



Recent Forum Posts

BWASL returns
dwirch posted on May 13, 2017 at about 15:24 in Site News

BWASL returns
dwirch posted on May 13, 2017 at about 8:46 in Site News

Job Spammer: Balashankar Bose Bose
dwirch posted on May 11, 2017 at about 10:05 in Spammers

Job Spammer: Bharti Jigyasi
dwirch posted on May 11, 2017 at about 7:58 in Spammers

List of Shady Characters
dwirch posted on April 25, 2017 at about 16:39 in Webmaster Stuff

Job Spammer: Bilal Uddin
dwirch posted on April 25, 2017 at about 11:00 in Spammers