Looking to remove special characters from Form String Classic ASP

asp-classic

I'm using Classic ASP, and pulling back the value of a users name from a form and assigning it to a local variable. I would like a way to parse the content and remove special characers such as ',+,@,!,- etc.

usrfname = Replace(Request.Form("FirstName"), "'", "")

I was able to use the above example to remove the ', but I would like to be able to remove multiple different characters as indicated above.

Best Answer

Most simply, you can just chain replaces to keep removing the characters you don't want:

usrfname = Replace(Request.Form("FirstName"), "'", "")
usrfname = Replace(usrfname, "+", "")
usrfname = Replace(usrfname, "@", "")

etc.

If you wanted a more elegant solution you could use a regular expression object. If you want to strip out everything apart from alpha-numeric characters, try:

Dim regEx
Set regEx = New RegExp
regEx.Pattern = "[^\w]"
regEx.Global = True
usrfname = regEx.Replace(Request.Form("FirstName"), "")

If that isn't quite what you're after then you can alter the regex to suit your exact requirements.

Related Topic