Skip to main content
Inspiring
June 2, 2008
Question

CFINPUT Datefield Type Validation

  • June 2, 2008
  • 5 replies
  • 1617 views
I've set up a <cfinput> with type="datefield". I'd like to have it work if the field is empty but would also like to valdidate an entry if made. Here's the code I am using:

<cfinput type="datefield" name="anniversary" value="#SelectAssociate.anniversary#" required="no" validateat="onSubmit" validate="date" message="Please enter the date as MM/DD/YYYY">

When I try and submit the form with the field empty I get the following error:

The value '' cannot be converted to a number.

This validates just fine if an entry is made both valid and invalid entries.

Can I accomplish my goal? If so what am I doing wrong?

Thanks.
    This topic has been closed for replies.

    5 replies

    Inspiring
    June 2, 2008
    Why would you validate a datefield at all? Is it possible to submit a form with something other than a date or empty string?
    drmavesAuthor
    Inspiring
    June 3, 2008
    I finally have it worked out. The problem was in the action page.

    I added a conditional statement to handle the empty field and everything works fine now.

    Thanks for everyone's help.
    Inspiring
    June 2, 2008
    Does the error occur when you load the page with the form, or when you submit the form?
    Inspiring
    June 2, 2008
    text input fields are ALWAYS defined, empty or not.
    you need to check for length of field value:
    <cfif len(trim(form.anniversary))>
    <!--- check that the value is a proper date here --->
    <cfelse>
    <!--- no data was entered by user - do whatever you need to do, if
    anything --->
    </cfif>

    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com/
    Inspiring
    June 2, 2008
    If you decide not to make the field mandatory then use the isDefined function on your action page:

    <cfif isDefined('anniversary')>
    <!--- Perform operations on anniversary --->
    <cfelse>
    <!--- Skip operations --->
    </cfif>

    EDIT: I completely spaced it; the code is incorrect. Azadi is right, if nothing is entered the field will exist but will be blank. Switch the if statement to the following and it should work as I had intended: <cfif anniversary is not "">
    drmavesAuthor
    Inspiring
    June 2, 2008
    Thanks for the suggestions.

    I can't make the field mandatory because it will reject the empty field.

    I do have processing in place on the action page to handle the empty field.

    The issue is that It is generating the error before any processing takes place in the action page. I think it must be happening in the javascript validation.

    Any more ideas?
    Inspiring
    June 2, 2008
    Either make the form field mandatory, or put some code into your action page to handle instances where the use opted not to select a date.