Skip to main content
Inspiring
March 11, 2024
Question

Invalid CFML construct found on line inside <script> tag

  • March 11, 2024
  • 2 replies
  • 472 views

Please find the below sample code,

 

<cfif local.fields[myName].readOnly>
<cfset thisAction = "view">
<cfelse>
<cfset thisAction = arguments.action>
</cfif>
 
<cfoutput>
 
<cfswitch expression="#thisAction#">
<cfcase value="edit">
<textarea id="#myName#"
name="#myName#"
rows="#local.fields[myName].rows#"
cols="#local.fields[myName].cols#"
onkeydown="maxlength#myName#(this,'#local.fields[myName].MaxLength#');maxlength1#myName#(this);"
onkeyup="maxlength#myName#(this,'#local.fields[myName].MaxLength#');maxlength1#FmyName#(this);"
>#IIF(len(local.fields[myName].value), DE(URLDecode(local.fields[myName].value)), DE(local.fields[myName].DefaultValue))#</textarea>
 
</cfcase>
 
<cfdefaultcase> 
<cfif LEN(local.fields[myName].value)>
<pre>#IIF(len(local.fields[myName].value), DE(URLDecode(local.fields[myName].value)), DE(local.fields[myName].DefaultValue))#</pre>
 
<cfelse>
<!--- nothing to display yet --->
<span style="font-style: italic;">N/A</span>
</cfif>
</cfdefaultcase>
</cfswitch>
 
<script type="text/javascript">
function maxlength#myName#(textarea,maxLength){
var mainString = textarea.value;
var len = mainString.length;
 
if (len >= maxLength) {
textarea.value = textarea.value.substr(0, maxLength);
return false;
}
 
}//end maxlength
 
function maxlength1#myName#(textarea){
str = textarea.value;
newStr = str.replace(/[&#]/g, "" ); // error 'Invalid CFML construct found on line' , occurs here
if(isNaN(newStr)){
newStr = "";
} else{
}
textarea.value = newStr;
}//end maxlength
</script>
</cfoutput>
 
My goal is : I should not be able to enter special characters '#' and '&'  in the textarea. I thought the following piece of code would stop me from entering # and & in the text area :
 
newStr = str.replace(/[&#]/g, "" );
 
However I'm getting error ,
 
Invalid CFML construct found on line newStr = str.replace(/[&#]/g, "" ); 
Please help me. 
 
Thanks,
Vishnu
    This topic has been closed for replies.

    2 replies

    Charlie Arehart
    Community Expert
    Community Expert
    March 12, 2024

    Vishnu, you have a couple of problems there. First the compilation error is because you're passing in as the first argument some characters (the regex) that form a string--and a string in cfml must be enclosed in quotes.

     

    More important, though, the cfml replace expression doesn't work with regular expressions. (Did you find some resource suggesting it would? Where is it?) 

     

    Instead, consider the rereplace and rereplacenocase functions. See the docs starting at https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-m-r/replace.html. See especially the first example, which you can also run online at the Cffiddle site. Then see still more on the topic at https://helpx.adobe.com/coldfusion/developing-applications/the-cfml-programming-language/using-regular-expressions-in-functions.html

     

    Let us know how things go. 

    /Charlie (troubleshooter, carehart. org)
    Legend
    March 11, 2024

    # is reserved for variable use in CF.  To use it in a regular expression in a function like you are, I believe you need to use a double-hash: ##  So,

     

    newStr = str.replace(/[&##]/g, "" );

    Inspiring
    March 12, 2024

    Thanks for replying,  it doesn't seem to be working. Is there any other way we can solve this?

    And can you please let me know what is \.\- in the below line of code ,

    newStr = str.replace(/[^0-9\.\-]/g, "" );
    Inspiring
    March 12, 2024

    I'm sory it works thank you!