Question

I'm building a form through which users will be able to submit articles. my current regex allows only certain characters and works as it should, though I do not know how to allow quotes as well. here is the code

<cfif refind ("[^A-Z a-z 0-9\\+\-\?\!\.\,\(\)]+", trim(form.articleText)) and len (trim(form.articleText)) gte 15>
     <cfset msg = "The article can not contain special characters.">
</cfif> 

I tried using &quot as in c# but it does not work!

Était-ce utile?

La solution

Add quotes in your character class:

<cfif refind ("[^A-Za-z0-9 +?!.,()\\""'-]+", trim(form.articleText)) ...

Autres conseils

anubhava's answer gives you what you've asked for, but the solution you probably need is actually something completely different: to use the ESAPI encodeForX functions in CF10 to encode the output appropriately for its context, such as encodeForHtml, instead of trying to restrict what characters can be written and constantly having to update it.

At most, you might want something such as:

<cfif refind('[[:cntrl:]]',form.articleText) >
    <cfset msg = "The article can not contain control characters.">
</cfif>

Which will block unprintable control characters, whilst not preventing perfectly reasonable characters such as accented letters, currency symbols, and so on.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top