Copy link to clipboard
Copied
I have a form that contains six input fields used for email addresses.
Once all six are filled out, I can create a concatenated string with the six input fields, separated by a comma, to be used in the cfmail to part.
What if they do not fill out all six input fields and only fillout two of the input fields with email addresses. ?
What is an efficient way to check if a field was filled out or not, with an email address ? My concatenated string will then only contain 2 input fields.
Copy link to clipboard
Copied
Hi,
try this,
<cfset concatenated="">
<cfif len(trim(form.fieldname))>
<cfset concatenated = concatenated & form.fieldname & ",">
</cfif>
for validating email
IsValid("email",(trim(form.fieldname)))
also ref :
http://livedocs.adobe.com/coldfusion/7/htmldocs/wwhelp/wwhimpl/common/html/wwhelp.htm?context=ColdFusion_Documentation&file=00000534.htm
Copy link to clipboard
Copied
Thanks for the reply.
So if I have six input fields, they would have different names, fieldname_1, fieldname_2, etc.
So your code would be :
<cfset concatenated="">
<cfif len(trim(form.fieldname_1))>
<cfif len(trm(form.fieldlname_2)>
etc...
<cfset concatenated = concatenated & form.fieldname1 & "," & form.fieldname2 & "," ....etc > ??
Copy link to clipboard
Copied
Here is a sample. Note I have not tested this code. Assumes that the text fields are consistently named as 'emailaddr_1' to 'emailaddr_6' and all are supplied on the post action of your form.
<cfset emailAddresses=ArrayNew(1) /> <!--- initialize array --->
<!--- loop the number of email form fields on your form --->
<cfloop from="1" to="6" index="idx">
<cfif IsValid("email", form["emailaddr_#idx#"])> <!--- ignore any values that are not email addresses, use array notation to get email_x value --->
<cfset ArrayAppend(emailAddresses, form["emailaddr_#idx#"]) />
</cfif>
</cfloop>
<!--- convert array to comma delimited list --->
<cfset emailList=ArrayToList(emailAddresses) />
Copy link to clipboard
Copied
Thanks for the reply.
So if I have six input fields, they would have different names, fieldname_1, fieldname_2, etc.
So your code would be :
<cfset concatenated="">
<cfif len(trim(form.fieldname_1))>
<cfset concatenated = concatenated & form.fieldname1 & "," >
</cfif>
<cfif len(trim(form.fieldname_2))>
<cfset concatenated = concatenated & form.fieldname2 & "," >
<cfif len(trim(form.fieldname_3))>
<cfset concatenated = concatenated & form.fieldname3 & "," >
</cfif>etc...
Copy link to clipboard
Copied
The suggestion by JR "Bob" Dobbs is more elegant. You could also use list functions and skip the arrayToList step .But that is a mainly matter of personal preference.
Also, do not forget about non-blank values that are _not_ a valid emails. You may wish to skip them, as in JR "Bob" Dobbs' example, or do something else.
emailaddr_1 = (blank)
emailaddr_2 = "foobar" <<---- this is invalid.
emailaddr_3 = "me@somewhere.com"
...