Reputation:
I am very new to ColdFusion and was curious if someone could tell me how to check to see if a form field is empty or not.
For example let's say we set it up like this:
<cfinput
type="text"
name="firstName"
id="firstName"
value="#form.firstName#"
>
How do I call this later to use it in another form? I tried many things but I am missing something somewhere.
<cfif (form.firstName) EQ 0>
Upvotes: 3
Views: 11225
Reputation: 11120
Some developers prefer checking for emptiness by checking comparing against an empty string. See len(x) better or x NEQ "" better in CFML?
<cfif trim(form.firstName) NEQ "">
<cfscript>
is also an option
<cfscript>
if (trim(form.firstName) != "") {
...
Yoda conditions work too
<cfscript>
if ( "" != trim(form.firstName)) {
Upvotes: 2
Reputation: 7397
The most straightforward way is:
<cfif form.firstName IS "">
It simply checks to see if the specified form field is an empty string ("").
Another way of writing the same thing would be:
<cfif len(form.firstName) EQ 0>
This checks to see if the length of the form field value is 0 (empty string). This second method can be shortened a little bit?
<cfif len(form.firstName)>
Assume that form.firstName is empty. This would then become . In boolean evaluation, 0 is false. Assuming the value was not empty, it would become . A non-zero number evaluates to true.
Upvotes: 3
Reputation: 544
I have always use a two fold check. IsDefined evaluates a string value to determine whether the variable named in it exists.
<CFIF NOT IsDefined("FORM.firstname") OR
FORM.firstname EQ "">
Reference: http://help.adobe.com/livedocs/coldfusion/8/htmldocs/help.html?content=functions_in-k_14.html
Upvotes: 7
Reputation: 14333
You can check if the length of the field is 0, using trim would remove any leading or trailing spaces.
<cfif len(trim(form.firstName)) EQ 0>
Upvotes: 14