I am trying to set an AMPscript function, that would check it the user provides a proper email name, in a proper format. If a user provides it in a wrong format, an error should appear.

For an error I will use "RaiseError" function, however, I don't know how to check if the structure of the email name string is correct.

The email name should be in the following structure: "xxxxx xxxxx - xxxx xxxx xxxx" - so the first part of the string should be separated by a dash from the second part of the string.

Which function would check if the user provides proper structure of email name?

I have already a function that catches for me the first part of the string (@sub_name):

var @index, @sub_name

Set @index = IndexOf(emailname_,' -')

set @sub_name = Substring(emailname_,1,Subtract(@index,1))    
  • 1
    Is the length of the individual elements (the xxx...) fixed or can it vary? – Stephan de Paly Jun 6 '19 at 10:13
  • 1
    Give us examples what would be correct and what would be wrong and why it would be wrong. Can it only be letters in front of the minus and after the minus or can this also be numbers? What makes an emailname correct. – Johannes Schapdick Jun 6 '19 at 11:08
  • 1
    Can you define the structure more precisely? Will there always be two words ("xxxxx") before the the dash? What about the number of words after the dash? – Rafał Wolsztyniak Jun 6 '19 at 11:13

Normally you would cover these aspects with RegExMatch. But your structure is totally unprecise. You need to exactly know how your valid strings look like. You can also see a sample of RegExMatch functions in an answer i have posted.

To get the stuff in front of your - i would use this. [But then there can only be one - in the whole email address and only once]. If fixed numbers of X then:

var @emailstring
var @subname1,@subname2,@subname3,@subname4,@subname5
var @valid

set @emailstring = 'Wonder fully-Emai name this'
set @subname1 = RegExMatch(@emailstring,'^(.{5})\s(.{5})-(.{4})\s(.{4})\s(.{4})$',1)
set @subname2 = RegExMatch(@emailstring,'^(.{5})\s(.{5})-(.{4})\s(.{4})\s(.{4})$',2)
set @subname3 = RegExMatch(@emailstring,'^(.{5})\s(.{5})-(.{4})\s(.{4})\s(.{4})$',3)
set @subname4 = RegExMatch(@emailstring,'^(.{5})\s(.{5})-(.{4})\s(.{4})\s(.{4})$',4)
set @subname5 = RegExMatch(@emailstring,'^(.{5})\s(.{5})-(.{4})\s(.{4})\s(.{4})$',5)

set @valid = 'false'
if(NOT Empty(@subname1) and NOT Empty(@subname2) and NOT Empty(@subname3) and NOT Empty(@subname4) and NOT Empty(@subname5)) then
set @valid = 'true'


Output then would be


not 100% sure if the times{4} parameter work in this, you could change a (.{5}) to (.....)

  • 1
    I guess this is more about the email assett name, not the address. – Stephan de Paly Jun 6 '19 at 10:48
  • funny sidefact. On CloudPages it did not work at first. Even though i changed nothing it worked after a while. i tested a lot with {} and [] and i was kind of stunned that these didn't seem to work. Somehow there is an issue with this which is kind of unreproduceable. Has anyone experienced same issues with {} [] and multiple . like ..... ? It was really really odd... – Johannes Schapdick Jun 6 '19 at 14:46

I came up with a ready solution:

var @sourceString, @regEx1 
set @sourceString = emailname_
set @regEx1 = ' - '

var @result2
set @result2 = RegExMatch(@sourceString, @regEx1, 0, 'IgnoreCase', 'Multiline')
if Length(@result2) > 0 
    set @check = 1 
    set @check = 0 

    @check == 0
RaiseError('Please provide email name in the following pattern: Subscription Name - Custom Name.', false)

  • 1
    This is not what you have been looking for.' - ' is a valid emailname and i highly doubt that this is what you have wanted. Furthermore you haven't set any capture group in your regex-pattern and you return the 0th parameter, capturing groups start with 1. Therefore i am not sure what 0 will return (maybe it returns the same as 1). Basicly ANYTHING with ' - ' in it will be valid. ' - Whatever' is valid aswell as 'Whatever - ' or '1 - 2'. Some RegExMatch can be seen on Eliot and Adams AMPScript-Guide.[If Adam or Eliots reads this, feel free gift me a copy – Johannes Schapdick Jun 6 '19 at 13:09

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.