ASP Classic CAPTCHA Customization VBScript Code Sample

The ASP Classic Captcha customization code sample shows how to customize BotDetect CAPTCHA behavior and appearance.

First Time Here?

Check the BotDetect Developer Crash Course for key integration steps.

BotDetect 3.0 allows user-defined customization of many Captcha options through a special BotDetect\CaptchaConfig.asp configuration file; some customizations also require page source changes.

Shown customizations include: CAPTCHA image color scheme, sound & reload icons and their tooltips, custom client-side handlers for BotDetect actions such as sound playing and Captcha reloading (resulting in simple message boxes on icon clicks in this sample), ...

The BotDetect\CaptchaConfig.asp file used in this sample project contains detailed descriptions and explanations of the many customizable options.

You can then use chosen customization options to configure BotDetect to precisely match your application requirements.

Download the BotDetect Classic ASP CAPTCHA Component and run this sample

Installed Location

By default, the Classic ASP Captcha customization code sample is installed at:
C:\Program Files\Lanapsoft\BotDetect 3 CAPTCHA Component\Asp\WebApp\CaptchaCustomizationSample

You can also run it from the BotDetect Start Menu:
Programs > Lanapsoft > BotDetect 3 CAPTCHA Component > ASP > Web Applications > Run

Default.asp

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.
w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
  <title>BotDetect ASP CAPTCHA Customization Sample</title>
  <link type="text/css" rel="Stylesheet" href="StyleSheet.css" />
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <!-- #include file ="BotDetect.asp" -->
</head>
<body>
  <form method="post" action="" id="form1">

    <h1>BotDetect ASP CAPTCHA Customization Sample</h1>
    
    <fieldset>
        <legend>ASP CAPTCHA validation</legend>
        <label for="CaptchaCode">Retype the characters from the 
        picture:</label>
        
        <% ' Adding BotDetect CAPTCHA to the page 
        Dim SampleCaptcha : Set SampleCaptcha = (New Captcha)("SampleCaptcha")
        SampleCaptcha.UserInputID = "CaptchaCode"
        Response.Write SampleCaptcha.Html %>
        
        <div class="validationDiv">
            <input name="CaptchaCode" type="text" id="CaptchaCode" />
            <input type="submit" name="ValidateCaptchaButton" 
            value="Validate" id="ValidateCaptchaButton" />
            
            <% ' CAPTCHA user input validation (only if the form was 
            submitted)
              If Request.ServerVariables("REQUEST_METHOD") = "POST" 
              Then
                Dim isHuman : isHuman = SampleCaptcha.Validate()
                If Not isHuman Then 
                  ' CAPTCHA validation failed, show error message 
                  Response.Write "<span class=""incorrect"">Incorrect 
                  code</span>"
                Else 
                  ' CAPTCHA validation passed, perform protected 
                  action
                  Response.Write "<span class=""correct"">Correct code
                  </span>"
                End If 
              End If
            %>
        </div>
    </fieldset>
    
    <script type="text/javascript">
      // custom javascript handler executed before Captcha sounds are played
      BotDetect.RegisterCustomHandler('PrePlaySound', function() { 
        alert('PrePlaySound: ' + this.Id); });
      
      // custom javascript handler executed before Captcha images are reloaded
      BotDetect.RegisterCustomHandler('PreReloadImage', function() { 
        alert('PreReloadImage: ' + this.Image.src + ' AutoReload: ' + 
        this.AutoReloading); });
      
      // custom javascript handler executed after Captcha images are reloaded
      BotDetect.RegisterCustomHandler('PostReloadImage', function() { 
        alert('PostReloadImage: ' + this.Image.src); });
    </script>
    
  </form>
</body>
</html>

The form follows the basic procedure for adding Captcha protection to ASP forms.

A script fragment defines custom code for client-side Captcha events defined in the BotDetect client-side object.

BotDetect\CaptchaConfig.asp

<%

' Captcha code configuration
' ---------------------------------------------------------------------

' LBD_Configuration_CodeLength:
' Captcha code length, in characters
LBD_Configuration_CodeLength = 4

' LBD_Configuration_CodeStyle:
' Captcha code style (alphanumeric, alpha, or numeric)
LBD_Configuration_CodeStyle = LBD_CodeStyles("Alpha")

' LBD_Configuration_CodeTimeout:
' Captcha code timeout in seconds - the Captcha can only be 
successfully solved within the specified time after generation. This 
is an optional security improvement that narrows the window of 
opportunity for attacks based on re-using the Captcha image on another 
site controlled by the attacker, or similar human-solver-based attacks 
on Captcha-protected forms.
LBD_Configuration_CodeTimeout = 600

' LBD_Configuration_Locale:
' Captcha locale string, affecting the charset used for Captcha code 
generation, and the pronunciation language used for sound Captchas
LBD_Configuration_Locale = "en-US"

' LBD_Configuration_CustomCharset:
' Defines a custom character set for Captcha code generation, 
specifying  the alphanumeric characters which will be used for all 
Captcha codes regardless of the codeStyle value set
LBD_Configuration_CustomCharset = "A,B,C,D,E,1,2,3,4"

' LBD_Configuration_BannedSequences:
' Defines rules about character sequences you want to avoid using in 
randomly generated CAPTCHA codes. Must be a CSV string.
LBD_Configuration_BannedSequences = ""


' Captcha image configuration
' ---------------------------------------------------------------------

' LBD_Configuration_ImageStyle:
' Captcha image style - can be a fixed or a randomized value selected 
from the 60 BotDetect image drawing algorithms
LBD_Configuration_ImageStyle = LBD_ImageStyles("Chalkboard")

' LBD_Configuration_ImageWidth:
' Captcha image width, in pixels
LBD_Configuration_ImageWidth = 250

' LBD_Configuration_ImageHeight:
' Captcha image height, in pixels
LBD_Configuration_ImageHeight = 50

' LBD_Configuration_ImageFormat:
' Captcha image output format (JPG, GIF, PNG or BMP)
LBD_Configuration_ImageFormat = LBD_ImageFormats("Gif")

' LBD_Configuration_CustomDarkColor:
' Used to override the default Captcha image color scheme. Html named 
or hex color value of the dark color used in Captcha images.
LBD_Configuration_CustomDarkColor = "#483D8B"

' LBD_Configuration_CustomLightColor:
' Used to override the default Captcha image color scheme. Html named 
or hex color value of the light color used in Captcha images.
LBD_Configuration_CustomLightColor = "#87CEFA"

' LBD_Configuration_ImageTooltip:
' Custom Captcha image alt text / title
LBD_Configuration_ImageTooltip = "CAPTCHA Image Custom English Tooltip"

' LBD_Configuration_HelpLinkEnabled:
' Is the Captcha help link enabled or not
LBD_Configuration_HelpLinkEnabled = True

' LBD_Configuration_HelpLinkMode:
' Controls how the Captcha help link is displayed; supported modes are "Image" 
' and "Text"
LBD_Configuration_HelpLinkMode = LBD_HelpLinkModes("Text")

' LBD_Configuration_HelpLinkUrl:
' Url (absolute or relative) to which the Captcha help link points to
LBD_Configuration_HelpLinkUrl = "captcha.html"

' LBD_Configuration_HelpLinkText:
' Text used in the Captcha help link; leave empty for default image width-
' dependent text
LBD_Configuration_HelpLinkText = "Custom Help Link Text"


' Captcha sound configuration
' ---------------------------------------------------------------------
' Captcha sounds in BotDetect 3.0 require locale-dependent 
pronunciation SoundPackage (.bdsp) files. Some default SoundPackages 
are included in all BotDetect installations, and pronunciations for 
additional languages can be downloaded from the Downloads section of 
the BotDetect website.


' LBD_Configuration_SoundEnabled:
' Is Captcha sound enabled
LBD_Configuration_SoundEnabled = True

' LBD_Configuration_SoundStyle:
' Captcha sound style - can be a fixed or a randomized value selected 
from the 10 BotDetect sound generation algorithms
LBD_Configuration_SoundStyle = LBD_SoundStyles("Industrial")

' LBD_Configuration_SoundFormat:
' Captcha sound output format (WavPcm16bit8KhzMono or 
WavPcm8bit8KhzMono)
LBD_Configuration_SoundFormat = LBD_SoundFormats("WavPcm16bit8kHzMono")

' LBD_Configuration_SoundTooltip:
' Custom sound Captcha icon alt text / title
LBD_Configuration_SoundTooltip = "Sound Icon Custom English Tooltip"

' LBD_Configuration_SoundIconUrl:
' Custom sound Captcha icon. You can use standard BotDetect icons (
"BotDetect\SoundIcon.gif" and "BotDetect\SmallSoundIcon.gif") or an 
icon of your own.

' LBD_Configuration_SoundPackageFolder:
' Custom sound packages folder. You can reuse a single location for 
all ASP applications using BotDetect Captcha on the same server, as 
long as the IIS worker process running them had Read permissions to 
the location
LBD_Configuration_SoundPackageFolder = "C:\Program Files (x86)
\Lanapsoft\BotDetect 3 CAPTCHA Component\Asp\Redistribute\BotDetectSounds"

' LBD_Configuration_SoundStartDelay:
' Starting delay of sound JavaScript playback, in miliseconds.
' Useful for improving usability of the Captcha sound for blind people using
' JAWS or similar readers, which will read the Captcha sound icon label when 
' it's selected and start sound playback simultaneously.
' Setting this delay to e.g. 2000 (2 seconds) will give the user time to hear
' both the pronounced label and the Captcha sound clearly.
LBD_Configuration_SoundStartDelay = 0

' LBD_Configuration_WarnAboutMissingSoundPackages:
' If the required SoundPackage in not deployed, the sound icon is not 
clickable and displays a warning tooltip by default. If you want to 
disable this warning and simply not display the sound Captcha icon at 
all for locales whose pronunciations are not deployed with the 
application, set this property to "False".
LBD_Configuration_WarnAboutMissingSoundPackages = True

' Captcha reload configuration
' ---------------------------------------------------------------------

' LBD_Configuration_ReloadEnabled:
' Is Captcha reloading enabled
LBD_Configuration_ReloadEnabled = True

' LBD_Configuration_ReloadTooltip:
' Custom reload Captcha icon alt text / title
LBD_Configuration_ReloadTooltip = "Reload Icon Custom English Tooltip"

' LBD_Configuration_ReloadIconUrl:
' Custom reload Captcha icon. You can use standard BotDetect icons (
"BotDetect\ReloadIcon.gif" and "BotDetect\SmallReloadIcon.gif") or an 
icon of your own.
LBD_Configuration_ReloadIconUrl = "http://captcha.com/images/refresh.png"

' LBD_Configuration_AutoReloadExpiredCaptchas:
' Captcha images are automatically reloaded when the Captcha code 
expires (as set in the LBD_Configuration_CodeTimeout element, or the 
ASP Session timeout), but only within a certain interval from their 
first generation. This allows you to have a short Captcha code timeout 
(e.g. 2 minutes) to narrow the window of opportunity for Captcha 
reusing on other sites or human-solver-powered bots, and actual 
visitors can still fill out your form at their own pace and without 
rushing (since the Captcha image will be reloaded automatically when 
it is no longer valid).
LBD_Configuration_AutoReloadExpiredCaptchas = True

' LBD_Configuration_AutoReloadTimeout:
' Since we don't want infinite sessions when the user leaves the form 
open in a background browser tab over the weekend (for example), we 
set a reasonable upper limit on the auto-reload period (e.g. 2 hours = 
7200 seconds).
LBD_Configuration_AutoReloadTimeout = 7200


' Captcha user input  configuration
' ---------------------------------------------------------------------

' LBD_Configuration_AutoFocusInput:
' The input textbox will be assigned focus on all Captcha Sound and 
Captcha Reload icon clicks, allowing the users to more easily type in 
the code as they hear it or as the new image loads. This does not 
apply to auto-reloading of expired Captchas, since the user might be 
filling out another field on the form when the auto-reload starts and 
shouldn't be distracted.
LBD_Configuration_AutoFocusInput = True

' LBD_Configuration_AutoClearInput:
' The input textbox will be cleared on all Reload icon clicks and auto-
reloads, since any previous input in the textbox will be invalidated 
by Captcha reloading. This is a small usability improvement that helps 
users avoid having to delete the previous input themselves.
LBD_Configuration_AutoClearInput = True

' LBD_Configuration_AutoLowercaseInput:
' Anything the users type in the input textbox will be lowercased on 
the fly, since Captcha validation is not and should not be case-
sensitive. This is a small usability improvement that helps 
communicate that fact to the users clearly.
LBD_Configuration_AutoLowercaseInput = True


' Captcha remote script configuration
' ---------------------------------------------------------------------------

' LBD_Configuration_RemoteScriptEnabled:
' Should BotDetect also add the remote JavaScript include loaded from the 
' captcha.com server. Currently used only for stats, but planned to develop
' into additional Captcha functionality.
LBD_Configuration_RemoteScriptEnabled = True


' Captcha persistence configuration
' ---------------------------------------------------------------------

' LBD_Persistence:
' By default, BotDetect stores generated Captcha codes and per-
instance set values in ASP Session state. If you want to change the 
persistence
'medium and use your own dictionary-type object, this is the setting 
to use.
Dim LBD_Persistence : Set LBD_Persistence = Session

' LBD_Configuration_UseApplicationFallback:
' Controls whether data required for sound Captcha playback will also 
be saved in ASP Application state, as a fallback for ASP Session state 
problems. Several browser/sound player combinations cause sound 
problems since they misplace ASP Session cookies when transferring the 
sound Url to the sound player (IE 7.0 + WMP 11, Safari 4 + QuickTime, 
etc.). Keeping this option set to 'True' makes sound Captchas play 
correctly even in such clients, at the cost of some server memory and 
slower execution due to Application state locking/unlocking necessary.
LBD_Configuration_UseApplicationFallback = True
%>

The ASP Captcha library configuration file defines base Captcha settings for the sample.