Click here to Skip to main content
Click here to Skip to main content

Tagged as

VBScript HTML Encode

, 4 Feb 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

John Doherty
Chief Technology Officer MammothWorkwear.com
United Kingdom United Kingdom
Senior Web Developer, Systems Architect and Entrepreneur. Technical Director of MammothWorkwear.com. More information is available at http://www.johndoherty.info/

Comments and Discussions

 
You must Sign In to use this message board.
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 1 PinmvpDave Kreskowiak19-Oct-09 12:44 
GeneralMy vote of 1 [modified] PinmemberMark Cilia Vincenti12-Oct-09 6:14 
GeneralSome problems, some options Pinmembermcnd5-Feb-09 5:31 
GeneralRe: Some problems, some options PinmemberJohn Doherty5-Feb-09 6:16 
GeneralRe: Some problems, some options PinmemberSpeednet_5-Feb-09 8:47 
GeneralRe: Some problems, some options PinmemberSaily25-Sep-14 11:33 
GeneralOnly &lt; &gt; & and " characters PinmemberDominic Pettifer4-Feb-09 13:55 
GeneralRe: Only &lt; &gt; & and " characters PinmemberJohn Doherty5-Feb-09 0:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150209.1 | Last Updated 4 Feb 2009
Article Copyright 2009 by John Doherty
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid