Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

VBScript HTML Encode

0.00/5 (No votes)
4 Feb 2009 1  
Server.HTMLEncode for VBScript (handles null strings)

Introduction

This function is a replacement for the Server.HTMLEncode method found in Classic ASP with one major difference... It accepts null strings without throwing errors!

The side effect is HTML Encoding for VBScript.

Background

I wrote this to overcome the common IsNull, IsNothing, IsEmpty string nightmare experienced when calling Server.HTMLEncode from Classic ASP.

Using the Code

Function HTMLEncode(ByVal sVal)

    sReturn = ""

    If ((TypeName(sVal)="String") And (Not IsNull(sVal)) And (sVal<>"")) Then
    
        For i = 1 To Len(sVal)
        
            ch = Mid(sVal, i, 1)

            Set oRE = New RegExp : oRE.Pattern = "[ a-zA-Z0-9]"

            If (Not oRE.Test(ch)) Then
                ch = "&#" & Asc(ch) & ";"
            End If

            sReturn = sReturn & ch
            
            Set oRE = Nothing
        Next
    End If
    
    HTMLEncode = sReturn
End Function
HTMLEncode("This is a & test!")

History

  • 4th February, 2009: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here