Click here to Skip to main content
Licence CPOL
First Posted 4 Feb 2009
Views 23,902
Bookmarked 7 times

VBScript HTML Encode

By | 4 Feb 2009 | Article
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)

About the Author

John Doherty

Chief Technology Officer
MammothWorkwear.com
United Kingdom United Kingdom

Member

Senior Web Developer/Systems Architect, One of the developers behind the 1999-2003 web based groupware application DeskVu/Convea. Presently CTO of MammothWorkwear.com, the UK's largest supplier of Dickies Workwear, Snickers Workwear and Caterpillar Workwear.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmvpDave Kreskowiak11:44 19 Oct '09  
GeneralMy vote of 1 [modified] PinmemberMark Cilia Vincenti5:14 12 Oct '09  
GeneralSome problems, some options Pinmembermcnd4:31 5 Feb '09  
GeneralRe: Some problems, some options PinmemberJohn Doherty5:16 5 Feb '09  
GeneralRe: Some problems, some options PinmemberSpeednet_7:47 5 Feb '09  
GeneralOnly < > & and " characters PinmemberDominic Pettifer12:55 4 Feb '09  
GeneralRe: Only < > & and " characters PinmemberJohn Doherty23:43 4 Feb '09  

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.

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