Skip to main content
Inspiring
January 29, 2009
Question

cfinput validate problem

  • January 29, 2009
  • 7 replies
  • 3262 views
Hi,
Does anyone know how to validate cfinput to not allow blank spaces as input? I want to only allow numbers, but people are just putting spaces in and it's throwing everything off.

Here is what I tried from the documentation I read, but it doesn't work:

<cfinput validate="float,noblanks" message="Week 3 must be numeric" type="text" name="week3">
    This topic has been closed for replies.

    7 replies

    January 30, 2009
    I'm not fun of client validation. It's always better to validate it on the server. So you can be pretty sure.
    Instead of 2 functions Trim and Len You can run following code

    March 13, 2009
    I am a similar problem with cfinput except I am validating a date. I use type="text" and validate="date". If the user enters 03/ /2009 it will allow it! If I use the regex validation I can prevent numbers but the user can enter invalid dates such as 03/32/2009 -- Anyone have this problem? Solved it? I used cfinput to try and avoid writing any extra javascript but cfinput is failing me. Any suggestions are appreciated.
    Inspiring
    March 13, 2009
    This looks like a bug. On my machine "03/ /2009" was treated as valid date by CFINPUT, however "03/32/2009" was correctly treated as invalid. I am on CF 8.0.1 on Windows.

    I am not a big fan of CF's out of the box client side validation features. You might want to look at a 3rd party Javascript validation library or you could write your own patch for the /CFIDE/scripts/cfform.js file on your server.


    Adobe's bug report page:
    http://www.adobe.com/cfusion/mmform/index.cfm?name=wishform
    Inspiring
    January 29, 2009
    Looking at your opening post, it didn't work because you didn't use it as described in the cfml reference manual.
    brianismAuthor
    Inspiring
    January 29, 2009
    Why does Coldfusion say you can use validate="noblanks" if it does not work?
    Inspiring
    January 29, 2009
    quote:

    Originally posted by: brianism
    Why does Coldfusion say you can use validate="noblanks" if it does not work?


    Note that the value of CFINPUT's required attribute defaults to "no" and as stated in the documentation "no: allows an empty field." To avoid allowing blanks add required="yes" to your CFINPUT tag as suggested by Bracuk.

    <cfinput required="yes" validate="float,noblanks" message="Week 3 must be numeric" type="text" name="week3">

    http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_i_07.html#1100379

    I also agree with Skinner that you should validate on the server. This will help you avoid problems when javascript validation has been bypassed or when javascript validation is insufficient.
    Inspiring
    January 29, 2009
    In our largest application, some js is made available to all pages. One of the functions is a trim. When required, we call it on the onChange attribute of the input.
    Inspiring
    January 29, 2009
    brianism wrote:
    > Thanks Dan. I tried this before but these input's are dynamic and some of them
    > are allowed to have nothing in them. Then I have code that deals with that. It
    > is when someone hits the space bar in the field that the problem occurs.
    >

    You can validate that on the server side after the submit post with the
    combination of len(trim()) functions. Trim will remove all white space
    characters and then len will return a length of zero. Throw an error on
    that state.

    On the client you can use regex to similar effect, but I'm not great at
    regex so I'll leave it to others to suggest the actual code. The
    internet is full of them.
    brianismAuthor
    Inspiring
    January 29, 2009
    Thanks Dan. I tried this before but these input's are dynamic and some of them are allowed to have nothing in them. Then I have code that deals with that. It is when someone hits the space bar in the field that the problem occurs.
    Inspiring
    January 29, 2009
    try validate="float" and required="yes"