Question about Coldfusion Regex

I currently have a coldfusion regex that checks if a string is alphanumeric or not. I would like to open this a little more to indicate period and underscore characters. How can I change this so that it can be done?

<cfset isValid= true/> <cfif REFind("[^[:alnum:]]", arguments.stringToCheck, 1) GT 0> <cfset isValid= false /> </cfif> 

thanks

+4
source share
3 answers

That should do it.

 <cfset isValidString= true/> <cfif REFind("[^[:alnum:]_\.]", arguments.stringToCheck, 1) GT 0> <cfset isValidString= false /> </cfif> 

Also, using "isValid" for a variable name is not a big practice. This is the name of the function in ColdFusion and may cause problems.

+3
source

There is no need for cfif - here is a nice concise way to do this:

 <cfset isValidString = NOT refind( '[^\w.]' , Arguments.StringToCheck )/> 


Alternatively, you can do it like this:

 <cfset isValidString = refind( '^[\w.]*$' , Arguments.StringToCheck ) /> 

(To prevent an empty line, change * to + )

This method can simplify the application of other restrictions (for example, it must start with a letter, etc.), and in any case it is a slightly more straightforward way to express the original check.

Please note that ^ here is an anchor meaning "start of line / line" (with $ - corresponding end), more information here .

+4
source

Will this work for you?

 refind("[\w\d._]","1234abcd._") 
0
source

All Articles