Can someone point me to some coding examples that show how to use ampscript to make a cloudpage form that will write to a data extension. I've been in the trailhead but it doesn't get detailed enough. I've tried Smart Capture forms but they don't have all the functionality I need - like checking to see if user is already in the DE and, if so, throwing out an error message.


Take a look at the Ampscript.Guide website for some solid examples of doing this function within a CloudPage. Ultimately, the ampscript function you'll be using is one of these: UpsertDE, InsertDE, or UpdateDE.

SmartCapture is pretty limited in function & style, in my experience.

Example: https://ampscript.guide/upsertde/

| improve this answer | |

Adding to all the above comments and answers.

Don't forget page security and data cleanliness. For security, trailhead actually has some very useful resources:


Follow standard best practices:

  • Don't let people gather information from your database on public webforms. Example: Do not auto-populate forms with data from any Data Extension once the user enter their email address in an attempt "to be more user-friendly". If I enter your email address, will I see your data?

  • Don't send unencrypted personal data in GET requests (commonly known as "normal links"). Use POST Requests where possible (between form and handler). From emails, only GET Requests are possible, so point to your cloudpage with CloudPagesURL

  • Before the next two steps, stop and think: What data format does your database expect?

Are you writing into any old data extension? Is this a process you want to repeat? Does the data have to align with other data in other DEs or other databases? Example: Are you entering the data into Sales Cloud? What's the character limit for e.g. "firstname" in Sales Cloud? Are you enforcing this? A more complex example: How do phone numbers have to be structured? Ensure you meet these demands, try not to create a mess with new logics everytime someone comes up with a webform.

  • Frontend validate form inputs for convenience. The user should get an understandable error message if they enter unexpected input. (think phone number formats...) (You can do this using standard web technology, e.g. the HTML5 pattern attribute, javascript, jQuery...)

  • Backend validate for security. As a general best practice, make sure the data input is actually formatted as expected before you process it further.

You can use RegexMatch (which leverages Regular Expressions/ RegEx) for this. here is an example for validating an email address using RegEx:

Assume a data extension "RegexPatterns" with the following fields. Note that both the regex for frontend validation (using HTML5) and AMPScript backend validation are in here. The formpage looks up the patternHTML5, the handlerPage looks up the patternAMPscript. The format of those two differs slightly (AMPScript needs leading ^ and trailing $)but it makes sense to have both in one place. For demonstration purposes, I used the HTML5 standard for email formatting. This might deviate from what you actually need.


You can write and test regex for marketing cloud AMPScript here: http://regexstorm.net/ as it uses the .net framework. HTML5: http://html5pattern.com

This can get you started and can be enhanced further. It would be code on your handler page that is done before any processing occurs:

%%[SET @Email = RequestParameter("email")

SET @patternForEmail = Lookup("ENT.RegexPatterns","PatternAMPScript","Fieldname","Email")

/* not empty check: only non-empty input is validated with the Regex.
A failed RegEx validation check results in @valid_Email being set to EMPTY.
no user input results in @valid_Email being set to "no_userinput". */

    SET @valid_Email = RegExMatch(@email, @patternForEmail, 0)
    SET @valid_Email = "no_userinput"

/* Logical test on the result of the string validation:
If the regex was successfully passed and the input field was not left empty,
@regexCheckvalid is set to true. This is used to determine whether processing continues or aborts.
This process can be used for MANDATORY fields.

If no user input should be a valid entry, then change the next IF Statement to:
IF not empty(@valid_Email) THEN

IF not empty(@valid_Email) and @valid_Email != "no_userinput" THEN
    SET @regexCheckValid = true ENDIF

IF @regexCheckValid == true THEN
    /* continue processing */
    /* validation error. display error message */
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.