Hello, I need to remove letters and characters from a request.querystring in order to convert it into an integer. This is only extra precaution from people screwing around in the address bar directly. I have enough protection in place to protect errors. Let's say that I want to retrieve a current location that is kept by an integer from the querystring.
http.//www.mydomain.com/direction.aspx?loc=2
This works great, but then if someone does this:
http.//www.mydomain.com/direction.aspx?loc=2sdf;DROP
or something like it, it just automatically redirects to loc=1. I was hoping that someone could help me figure out how to request.querystring("loc") then pull out the invalid characters to keep it a string. Thinking off the top of my head, this is all I can think of which is FAR too lengthy. I know there is a shorter way:
Function MakeInteger()
Dim i As Integer
Dim loc As String = request.QueryString("loc")
Do While Not IsNumeric(loc)
loc = Replace(loc, "a", "")
loc = Replace(loc, "b", "")
...
...
loop
Return (loc)
End Function
Is there a way to do something like: loc = Replace(loc, [A-z], "") ?
I found this in C# I believe. Maybe someone who knows how to validly convert it to VB? Thanks
StringBuilder sb = new StringBuilder ();
for (int i=0; i< string.Length; i++)
if (char.IsLetterOrDigit(string[i])
sb.Append(string[i]);