How to make numbers output as text in Excel export

Feb 29 2012

This is most useful when you have a zip code from the East such as 04567 in your database and you want to export it to Excel. Once exported it will look like 4567.
An easy fix for this problem is to use a Microsoft style with the table element such as this


<td width="155" style="mso-number-format:\@">#numberformat(zipcode, "00000")#</td>

Comments Off

Reading json from a ColdFusion Query

Jul 08 2011

Working for a client who has Coldfusion version 6.1. After a day of trial and error I finally got this working. Reading a json reply from a coldfusion cfc query.

  1. Fist of all the cfc function must have access=”remote” output=”false” returntype=”any” returnformat=”json” in the cffunction line.
  2. Return a json string from the function like this:jsonreturn=’{“address_1″: “#qFirmAddress.address_1#”,

    “address_2″:”#qFirmAddress.address_2#”,

    “city”:”#qFirmAddress.city#”,
    “state”:”#qFirmAddress.state_province#”,

    “zip”:”#qFirmAddress.zip#”}’

  3. Then in the jQuery code you have to do this in the success function:

    success:function(resp){
    var start =resp.search("{")-1;
    var end= resp.search("}")+1;
    var myjson =resp.substring(start,end);
    var obj = jQuery.parseJSON(myjson);
    var addr1=obj.address_1;
    var addr2=obj.address_2;
    var city=obj.city;
    var state=obj.state;
    var zip=obj.zip;}//end success

Comments Off

How to call a CFC from jQuery ajax

Jun 17 2011

Posting how to call a cfc from jQuery, sending in form data an easy way. I just finally got this to work and sure I will use it again.


<--- Start of popup--->
<div id="dialog-pause<cfoutput>#dnum#</cfoutput>" title="Pause Counter">

<p>Counter restart date can be entered if you desire.</p>
<form action="reminder" method="post" name="pauseForm" id="pauseForm">
<input type="text" name="pauseresetdate" id="pauseresetdate<cfoutput>#dnum#</cfoutput>" >
</form>
</div>
<--- End of popup--->

<--- Start of Script--->
<script type="text/javascript" language="javascript">
jQuery().ready(function() {
jQuery( "#dialog:ui-dialog" ).dialog( "destroy" );
jQuery("#dialog-pause<cfoutput>#dnum#</cfoutput>").dialog({
autoOpen: false,
height: 200,
width: 250,
modal: false,
buttons:{
"Pause": function() {

jQuery.ajax({
type: "POST",
url: "/objects/reminders.cfc?method=pauseResetReminder",
data: {
rmdid: "<cfoutput>#RMD_ID#</cfoutput>",
restartdate: jQuery("#resetdate<cfoutput>#dnum#</cfoutput>").val(), //how to get form field data
pause: "0"
},
dataType: "json",
success: function(data) {
location.href="maintenance-reminders";
}

});
},
"Cancel": function() {
jQuery( "#dialog-pause<cfoutput>#dnum#</cfoutput>" ).dialog( "close" );

}
}
});
jQuery( "#dialogpauseButton<cfoutput>#dnum#</cfoutput>" ).click(function() {
jQuery( "#dialog-pause<cfoutput>#dnum#</cfoutput>" ).dialog( "open" );
});
});
</script>

Comments Off

How to verify an Email Domain in Java and Coldfusion

Dec 29 2009

Often people will submit junk email addresses such as wwqewq@dasds.com . While this method is not perfect because some email sites do not allow query and some SMTP sites are hidden, it will allow you to at least mark these addresses as potentially bad. I usually will save them in a pending table and manually check them once in a while.

Here is a Java class that can do this job for you.

import java.util.Hashtable;

import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

public class AddressCheck {
private String addr;
private boolean isLegitAddress;

public AddressCheck() {
}

public String getAddr() {
return addr;
}

public void setAddr(String addr) {
this.addr = addr;
}

public boolean isLegitAddress() {
return isLegitAddress;
}

public void setLegitAddress(boolean isLegitAddress) {
this.isLegitAddress = isLegitAddress;
}

public boolean isReachable(){
Hashtable env = new Hashtable();
env.put(“java.naming.factory.initial”,”com.sun.jndi.dns.DnsContextFactory”);
DirContext ictx;
try {
ictx = new InitialDirContext(env);
Attributes attrs = ictx.getAttributes(getAddr(), new String[] { “MX” });
Attribute attr = attrs.get(“MX”);
if (attr == null) {
setLegitAddress(false);
} else {
setLegitAddress(true);
}
} catch (NamingException e1) {
setLegitAddress(false);
}
return isLegitAddress();

}
}

This can be converted to be used in Coldfusion as well but a word of warning. This will not run on sites that are only running Coldfusion using the JRE. The site must also use the Java SDK. If you are using a shared environment on a host site, it probably will not work. I have tried it on two common hosting sites and they were unwilling to install the JDK. It does work on my local machine where I run CF 8 under tomcat.
You can add this function to a cfc:
<cffunction name=”isDomainValid” returntype=”boolean” output=”true”>
<cfargument name=”emailaddress” required=”Yes” type=”string”>
<cfset atLoc=#Find(“@”,arguments.emailaddress)#>
<cfset env = CreateObject(“java”, “java.util.Hashtable”)>

<cfset env.put(“java.naming.factory.initial”, “com.sun.jndi.dns.DnsContextFactory”)>
<cfset dirContext = CreateObject(“java”, “javax.naming.directory.InitialDirContext”)>

<cfset dirContext.init(env)>
<cfset valid=false>
<cftry>
<cfset env.put(“java.naming.provider.url”, “google.com”)>
<cfset env.put(“com.sun.jndi.dns.timeout.initial”, “2000″)>
<cfset env.put(“com.sun.jndi.dns.timeout.retries”, “3″)>
<cfset type = ArrayNew(1)>
<cfset type[1] = “MX”>
<cfset attributes = dirContext.getAttributes(“#Mid(email, atLoc + 1, Len(email))#”, type)>

<cfset atribEnum = attributes.get(“MX”)>
<cfset enum =#atribEnum.getAll()#>

<cfloop condition=”#enum.hasMore()#”>
<cfset attribute = Enum.next()>
<cfset valid=true>

</cfloop>
<cfcatch>
<cfset valid=false>
</cfcatch>
</cftry>
<cfreturn valid>
</cffunction>

Comments Off