Using AMPscript to Validate Fields in a Landing Page

This document contains conceptual and procedural information about using AMPscript to validate fields in a landing page.

Prerequisites

You must have landing pages enabled for your account. Contact your ExactTarget representative if you have any questions about using landing pages with your ExactTarget account.

Why Use Validation

Validation ensures that your subscribers enter correctly formatted information in your landing page. You can set rules governing the data entered in your landing page fields that prevent entries with too few or too many characters (such as a ZIP code or phone number) or force subscribers to enter data in a required field.

How to Use Validation AMPscript in Your Landing Page on LOAD

First, you must create variables in your landing page. In this example, the code includes two variables and sets @zip to the value entered in a landing page field called "ZIP."

<html>
%%[ Var @zip, @redirURL
Set @zip = RequestParameter("zip")

The next part of the code evaluates the number of characters included in the value of @zip. If there are five characters, it passes the variable to the page.

/* We have 5 characters */
if Length(@zip) == 5 then
    Set @redirURL = Concat("http://exacttarget.com?zip=",@zip) ]%%
    <head>
        <META http-equiv="refresh" content="0;URL=%%= v(@redirURL) =%%" />
    </head>
    <body>
    </body>

If there are less than five characters in the page, it prompts the subscriber for a correct entry:

%%[ elseif Length(@zip) < 5 then ]%%
    <head>
    </head>
    <body>
        <h1>Show the form again and prompt for the correct number of characters</h1>
    </body>

Finally, if the subscriber fails to enter a ZIP code, the landing page prompts them for an entry:

%%[ elseif Empty(@zip) == "true" then ]%%
    <head>
    </head>
    <body>
        <h1>Show the form again and state the zip code field is required</h1>
    </body>
%%[ endif ]%%
</html>

How to Use Validation AMPscript in Your Landing Page on POST

If you want your AMPscript to validate the information on POST and not LOAD, you can include the following elements in your form.

First, build the form and HTML according to your needs. In the body of the page, include the following form tag:

<form type="POST" action="%%= RequestParameter('PAGEURL') =%>

This ensures that when the form posts, it goes back to the form. The form should also include two hidden fields:

<input type="hidden" __executionContext="POST">
<input type="hidden" __contextName="fieldName">

You can also include the following two optional hidden fields. These fields point the subscriber to different pages depending on whether the form was completed successfully or incorrectly.

<input type="hidden" __errorPage="http://example.com/error">
<input type="hidden" __successPage="http://example.com/success">

Finally, include the following AMPscript in your page:

%%[[name="fieldName";type="POST"]
VAR @zip
SET @zip = RequestParameter("zip")
IF Empty(@zip) == "true" THEN
     RaiseError("Please enter a valid ZIP code.")
ENDIF 
]%%

 


Enter the digits 25380 backwards:
   
 

 

 

Tag page
You must login to post a comment.