Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function to check if a variable name is valid

I have some code using the SetVariable function which takes the first parameter as a variable name created based on a variable name passed to this code.

I'm looking for a way to check the string passed to it is a valid variable name using a function rather than using a try/catch block to trap this error.

I know there's a function IsValid but this only checks if the variable is valid, not if it's a valid variable name. Does anyone have any suggestions or custom code they've used to do this?

Thanks,

Tom

like image 318
Loftx Avatar asked Jul 15 '11 08:07

Loftx


People also ask

How do you know if a variable is valid?

A valid variable name begins with a letter and contains not more than namelengthmax characters. Valid variable names can include letters, digits, and underscores.

How do you find the invalid variable name?

Variable name may not start with a digit or underscore, and may not end with an underscore. Double underscores are not permitted in variable name. Variable names may not be longer than 32 characters and are required to be shorter for some question types: multiselect, GPS location and some other question types.

What makes a variable name valid?

A valid variable name starts with a letter, followed by letters, digits, or underscores.

Which is a valid variable name in R?

A valid variable name consists of a combination of alphabets, numbers, dot(.), and underscore(_) characters. Example: var. 1_ is valid. Apart from the dot and underscore operators, no other special character is allowed.


1 Answers

I know there's a function IsValid but this only checks if the variable is valid, not if it's a valid variable name

Actually IsValid() will do what you want if you specify "variableName" as the type.

<cfoutput>#IsValid( "variableName","1")#</cfoutput>

Will output NO, since variable names in CF cannot begin with a digit.

like image 175
CfSimplicity Avatar answered Oct 02 '22 19:10

CfSimplicity