3

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>
Leigh
  • 28,765
  • 10
  • 55
  • 103
  • 3
    There's no reason to use `cfinput` use regular `form` and `input` tags to create your HTML. – Matt Busche Jan 16 '15 at 20:47
  • Aside from a direct answer to your question, you *always* want to make sure the field exists first, since checking the value of a non-existent field in CF is an error. Even if you think it must always exist, a user could use their browser dev tools to alter your form before submitting. So you could use something like ``. Or you could use `` to give it a default value. – Josh Jan 16 '15 at 21:53
  • In regards to the comment above about no reason to use cfinput. While I do not use cfinput it should be noted that the tag does apply HTML Formatting. So if the value of form.firstname contains a non friendly HTML character then the tag automatically takes care of that. Otherwise if that code were changed to a plain input then it should at least be also changed to value="#HTMLEditFormat(form.firstName)#" – Snipe656 Jan 19 '15 at 15:30

4 Answers4

14

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>
Matt Busche
  • 14,216
  • 5
  • 36
  • 61
  • 1
    *remove any leading or trailing spaces.* .. and [control characters](https://wikidocs.adobe.com/wiki/display/coldfusionen/Trim) FWIW – Leigh Jan 16 '15 at 21:19
7

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

Mitch
  • 554
  • 2
  • 17
  • 2
    `StructKeyExists()` would be a better option. – Pankaj Jan 19 '15 at 05:41
  • I am not necessarily disagreeing with you, but can you support this with some documentation? – Mitch Jan 19 '15 at 05:56
  • I mean instead of `isDefined()` you should use `StructKeyExists()`. Please read this https://forums.adobe.com/thread/491991?tstart=0 – Pankaj Jan 19 '15 at 07:34
  • Interesting. Sounds like it could be more efficient. I have never had issue with isDefined() like the guy claims he had in your link, however. Thanks! – Mitch Jan 19 '15 at 16:17
3

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.

David Brierton
  • 6,977
  • 12
  • 47
  • 104
  • In ColdFusion 9, throws this error, "Invalid CFML construct found on line 107 at column 17. ColdFusion was looking at the following text: ?" – Dan Bracuk Jan 17 '15 at 15:50
  • 1
    David, two things: `len(variable)` and `len(variable) eq 0` are the opposite (but you can use `not len(variable)`). Also Dan is obviously right, but the ? and " are neighbors on the keyboard, you may have just made a simple typo. Either way, it's worth correcting – Regular Jo Jan 17 '15 at 17:28
2

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)) {
Community
  • 1
  • 1
James A Mohler
  • 11,060
  • 15
  • 46
  • 72
  • While this code sample may possibly answer the question, it would be preferable to include some essential explanation to your answer. As it stands now this answer adds little to no value for future readers. – oɔɯǝɹ Jan 31 '15 at 11:10