1

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))    
3
  • 1
    Is the length of the individual elements (the xxx...) fixed or can it vary? Commented Jun 6, 2019 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. Commented Jun 6, 2019 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? Commented Jun 6, 2019 at 11:13

2 Answers 2

3

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'
endif
]%%

%%=v(@valid)=%%<br>

Output then would be

true

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

2
  • 1
    I guess this is more about the email assett name, not the address. Commented Jun 6, 2019 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... Commented Jun 6, 2019 at 14:46
0

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 
then 
    set @check = 1 
else 
    set @check = 0 
endif

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


]%%
1
  • 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 Commented Jun 6, 2019 at 13:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .