I have a function which searches users by their e-mail. If a user is found, the function returns the userid. Otherwise it returns false.
How may I evaluate this userid which looks like 12202436120g1200069971 to true?
<cfif userid NEQ false>
// do something
<cfelse>
// do something else
</cfif>
If I understand correctly I'd do something like:
if(len(userid)){
//true
}
else{
//false
}
In ColdFusion 0 is false and any other number (negative or positive) is true.
Related
In ColdFusion, isValid() with a type of "telephone" validates a US phone number. However, it's not just looking at the number of digits.
For example:
isValid( "telephone", "8002345678" ) // returns true
isValid( "telephone", "1111111111" ) // returns false
Besides the number of digits, what rules or regex does CF use to validate a phone number?
We can able to validate US number using isValid() function.
<cfoutput>#isValid("regex", "(123) 123-1234","^(\([0-9]{3}\) |[0-9]{3}-)[0-9]{3}-[0-9]{4}$")#</cfoutput>
<!--- format (123)123-1234 --->
<cfoutput>#isValid("regex", "(123)123-1234","^(\([0-9]{3}\)|[0-9]{3}-)[0-9]{3}-[0-9]{4}$")#</cfoutput>
<!--- format 123-123-1234 --->
<cfoutput>#isValid("regex", "123-123-1234","^[0-9]{3}-[0-9]{3}-[0-9]{4}$")#</cfoutput>
Thanks,
Coldfusion 8 version here.
Here is a snipset of my code:
<cfset ColumnNames = structKeyArray(ApiData[1])>
<cfdump var="#ColumnNames#"><!--- lowercase names --->
<cfdump var="#ArrayToList(ColumnNames,",")#"> <!--- need each name in Array in UPPERCASE --->
uCase(ColumnNames) wont work. Do I have to loop it through each item and use uCase?
Thank you
Or even just turn it into a list first using structKeyList(), which you can call uCase() on.
<cfset ColumnNames = uCase(structKeyList(ApiData[1]))>
It is ugly, but you could do:
listToArray( UCase( structKeyList( ApiData[ 1 ] ) ) )
I'm currently trying to validate if a string is exactly true or false as I'm building a spreadsheet importer using CF10.
My users will either enter in one of the following.
NULL, which will equate to false
TRUE which obviously equates to true
FALSE which again will equate to false.
I'm trying to use the isValid('boolean',variable.data) to validate if the data is in fact a boolean string or not. However after reading quite a few posts I can see that it will validate true if its a positive number and false if it is a negative one etc.
I'm wondering if anyone has an easy fix to getting boolean validation working for strings easily ? Is this more a regular expressions scenario to make it work properly ?
Any help greatly appreciated.
Assuming you mean the literal string "NULL", one option is using list functions. Search a list of allowed values, ie "true,false,null". If the entry is found, it is valid:
<cfif listFindNoCase("true,false,null", theValue)>
this is a valid boolean value
<cfelse>
not found. do something here...
</cfif>
Then a simple string comparison would return false for everything other than the literal string "true":
isTrue = compareNoCase(e, "true") eq 0 ? true : false`
If you need to evaluate whether a string is exactly "true" or "false", then you are not doing a boolean comparison, you are doing a string comparison.
So to that end, you'd be wanting to use compare() or compareNoCase() (depending on how rigid you need to be).
You could just do some logic or am I missing something.
<cfset booleanResult = enteredValue eq 'TRUE' ? true : false />
I am converting a ColdFusion application to C# (I'm a CF n00b).
I have a script that performs a cfquery and then cfloop's through the results, and it appears to be trying to compare the current row to its following row. And it appears to be trying to make sure that it doesnt try to read past the end of the array.
<cfquery name="qTripLegs" datasource="#sdb#">
SELECT ...
</cfquery>
<cfloop query="qTripLegs">
<cfif (customs_stop[currentrow] NEQ "" OR fuel_stop[currentrow] NEQ "") AND recordcount GT currentrow AND departure[currentrow] NEQ arrival[currentrow+1]>
It feels like currentrow is 1-based (currentrow will have a value of 1 when it first enters the cfloop). Am I correct? I have looked in the coldfusion documentation and I dont see anything about this.
Yes, queries and arrays in CF are 1-based.
The CurrentRow and RecordCount variables are properties of the query (inside a query loop they are automatically scoped).
<cfloop query="QueryName">...</cfloop> will loop through the entire query*, from 1 to QueryName.RecordCount, and the QueryName.CurrentRow index is automatically populated/incremented appropriately. Its value prior to query loop isn't used.
*(unless cfbreak/etc used)
Also to point out there is generally no need to prevent reading past the end (as above, the query loop handles it), it's only because CurrentRow+1 is being used that it's needed to avoid an error.
query.currentRow()
Returns the current row number
queryCurrentRow(query) → returns numeric
Member Function Syntax
<cfscript>
var myQuery = queryNew("id,title","integer,varchar",[[1,"Charlottes Web"],[3,"The Outsiders"],[4,"Mieko and the Fifth Treasure"]]);
cfloop(query = "myQuery"){
if (title Eq "Mieko and the Fifth Treasure"){
writeOutput(myQuery.currentRow());
}
}
</cfscript>
I normally don't work in ColdFusion but there's a FTP process at work I have to create a report for with the only option right now being a ColdFusion 8 server. This FTP feed has a few issues (trash too).
So, I make the query and then I need to convert some of the string values during the output to do some math. Before that:
How do I tell if a field in the output loop: is not blank or null, is string that can be converted into a valid number, and is not 0?
Is there a simple way of doing this w/o a lot of if statements?
Thanks!
So you want to make sure that the variable is numeric but not zero?
Then you want this:
<cfif IsNumeric(MyVar) AND MyVar NEQ 0 >
Determining if a string is not null/blank and is a number and not 0?
Here's the code I would use in this case.
<cfif isDefined(stringVar) and len((trim(stringVar))) and isNumeric(stringVar)>
do stuff here
</cfif>
isDefined returns a true if the variable exists. If you know the scope of the variable, i.e., its in the form or url scope for instance, you can use structkeyExists(form,"stringVar"). I would recommend using this approach if you know the scope of the variable.
Len(trim(stringVar)) is the second check. First off it trims any leading or trailing empty spaces from the string - this makes sure that any empty variables are not passed along. Then if something is there it will return the length of the string. If its empty len will return a 0.
isNumeric(stringVar) returns a true if the variable is a number and false otherwise.
<cfif Len(field) and Val(field)>
Len() will verify the field has length (not blank--there are no NULLs in CF) and Val() will automatically convert the first character in the string into into a number--or return 0 if it cannot.
Take note of Peter's comment below; although this is the least verbose answer, Val() may fail in certain edge conditions below, ie. The field is a string but starts with a number, incorrectly converting it to a number, and evaluating to TRUE.
<cfif isNumeric(myfield) and myfield gt 0>