Click to See Complete Forum and Search --> : Simple string validation


Booma
04-02-2008, 05:45 AM
Hello everyone,

I'm validating a query string to make sure it's an Integer like this:

If Not IsNumeric(Request.QueryString("course")) Then do something else

Which works fine if the query string is a mixture of Intergers and letters, (46fhh or e5) for instance.

However if the query string contains something like a full stop or a comma, (4. or 5ty/ or 6,) for instance, the statement returns true.

I'd like it to return false if it contains anything but an Integer.

Any ideas would be appreciated.

Many Thanks

cconn64
04-02-2008, 08:55 AM
I'd suggest looking into string replacements. So that anything that wasn't a number gets dumped.
Kinda like this:
Dim var1
var1 = Replace(Request.QueryString("course"), "\\", "", 1, -1, vbTextCompare)
var1 = Replace(var1, "/", "", 1, -1, vbTextCompare)
' Repeat the previous line for all the anticipated characters you can think of you might need to replace.

Alternatively, you could have a simple loop through a string that contains a list of characters that you want to remove. Like this:
Dim var1
Dim n = 1
Dim curCh
Dim myBadChars = "\\./,$!"
var1 = Request.QueryString("course")
While n <= Len(myBadChars)
curCh = Mid(myBadChars, n, 1)
var1 = Replace(var1, curCh, "", 1, -1, vbTextCompare)
Wend

Pardon if the coding is 100% accurate. I haven't done VBScript for 5-6 years and I'm a little rusty.