In these days, some people test writing HTML code, especially javascript tags, marquee tag and so on in input. Because of this case, we need to solve this problem not be inserted miscellaneous codes in our Database. I’ve prevented this kind of problems before time. But, it cannot be used as global function. That’s why I keep searching any solutions in Google and create own coding. Eventually, I’ve get following coding.
<cfscript>
function listFix(list) {
var delim = “,”;
var null = “NULL”;
var special_char_list = “\,+,*,?,.,[,],^,$,(,),{,},|,-,<,>”;
var esc_special_char_list = “\\,\+,\*,\?,\.,\[,\],\^,\$,\(,\),\{,\},\|,\-,<,>”;
var i = “”;
if(arrayLen(arguments) gt 1) delim = arguments[2];
if(arrayLen(arguments) gt 2) null = arguments[3];
if(findnocase(left(list, 1),delim)) list = null & list;
if(findnocase(right(list,1),delim)) list = list & null;
i = len(delim) – 1;
while(i GTE 1){
delim = mid(delim,1,i) & “_Separator_” & mid(delim,i+1,len(delim) – (i));
i = i – 1;
}
delim = ReplaceList(delim, special_char_list, esc_special_char_list);
delim = Replace(delim, “_Separator_”, “|”, “ALL”);
list = rereplace(list, “(” & delim & “)(” & delim & “)”, “\1″ & null & “\2″, “ALL”);
list = rereplace(list, “(” & delim & “)(” & delim & “)”, “\1″ & null & “\2″, “ALL”);
return list;
}
</cfscript>
It’s simple though. Save following coding as ListFix.cfm file. And, include this file before saving users’ data to your Database.
How easy? Try it.