Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Invalid CFML construct found on line inside <script> tag

Explorer ,
Mar 11, 2024 Mar 11, 2024

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
504
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 11, 2024 Mar 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, "" );

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 11, 2024 Mar 11, 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, "" );
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 12, 2024 Mar 12, 2024
LATEST

I'm sory it works thank you!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 11, 2024 Mar 11, 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-regul...

 

Let us know how things go. 


/Charlie (troubleshooter, carehart. org)
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources