1

Is it possible to add something to this function that will set up the date format and require the user to enter MM/DD/YYYY? MASK is not working..

Fee Calculator Function:

function getDatePrice() { 
      var datePrice = 0;
      var theForm = document.forms.form;      
      var purchasedate = theForm.elements.purchasedate; 
      var date = new Date(purchasedate.value);
      if (Object.prototype.toString.call(date) !== '[object Date]') {
        date = new Date();
      }
      var today = new Date();
      var diffMilli = today - date;
      var diffDays = Math.floor(diffMilli / 1000 / 60 / 60 / 24); // ..Divide!
      if (diffDays > 30) {
        datePrice = 20;
      }
      else {
        datePrice= 0;
      }
      return datePrice;
    }

Calling Function:

function calculateTotal()
{
    var titleFees = getDatePrice();
    var divobj = document.getElementById('totalPrice');
    divobj.style.display='block';
    divobj.innerHTML = "Estimated Transfer Fees $"+titleFees;
}

Input Button: (Requiring ColdFusion):

<cfinput
       type="datefield"
       name="purchasedate"
       width="130"
       required="yes"
       message="Please enter purchase date."
       value="#dateformat(now(),"mm/dd/yyyy")#" 
       oninput="calculateTotal();"
       >
  • the mask attribute sets up validation for user input through JS. Your "calculate" function may be preempting that. that's my guess. – Mark A Kruger Oct 09 '14 at 19:47
  • 1
    _I do not believe my JavaScript function is erasing the mask or giving it issues_ - Try a simple test, remove the entire attribute `oninput="calculateTotal();"` from your cfinput and see if it behaves differently. – Miguel-F Oct 09 '14 at 20:13
  • yes taking out the oninput still does not work =/ –  Oct 09 '14 at 20:16
  • I feel like maybe you can not mask a date field or that it has to be formatted some other way –  Oct 09 '14 at 20:17
  • 3
    According to [the docs you can](http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec0ffd8-7ffe.html#WSc3ff6d0ea77859461172e0811cbec0ffd8-7fe7). the last example on that page is using the same mask that you are. Are you using a _Flash_ form? I think the `datefield` type is specific to Flash forms. If you are not using a Flash form than try switching to a regular `"text"` input and change your mask to something like `"99/99/9999"` to test. – Miguel-F Oct 09 '14 at 20:26
  • No I am not using a flash form and everything is working properly except for that –  Oct 09 '14 at 20:30
  • I thought it would work too :/ That's why I am wondering if anyone has a workaround or something I may be able to fix it with –  Oct 09 '14 at 20:46
  • 3
    Miguel's suggestion about a text field and numeric mask will probably work. I used to do something like that but stopped. When you pre-populate a text field with a mask attribute as you are doing, attempting to change the value becomes difficult, unintuitive, and frustrating. To see what I mean, do what he says, and then try to change just the day portion of the pre-populated value. – Dan Bracuk Oct 09 '14 at 22:03
  • 2
    I want to make the date field work though because I like the fancy calendar button next to the text box –  Oct 10 '14 at 12:18
  • (Edit) Zane - I think Miguel-F is right. Masks probably are not supported outside of flash forms. Also, I agree with @DanBracuk on masks. I have seen very few that even work consistently. They are great in theory, but in practice - they usually end up frustrating the heck out of users. Another option is to use hints. Display some default text that disappears when you start typing. If you go that route, you should probably switch to jQuery instead of cfinput. – Leigh Oct 10 '14 at 15:55
  • What is strange is that I have others that actually work like.. `` but for some reason it is just the datefield that will not except the MASK –  Oct 10 '14 at 17:11
  • Hm... I have not used cfinput in a while, so I am not sure whether it really supports masks or not (outside of flash). Maybe that changed in later versions? What version of CF are you using? – Leigh Oct 13 '14 at 13:02
  • I am using CF10 ... It does work for cfinput if its a textbox, but for some reason not a datefield ... I searched the web for ColdFusion date field not working and many hits come back but no real solution on how to fix it that I have found. –  Oct 14 '14 at 13:21
  • @Leigh, according to the docs, `datefield` is supported for HTML as of CF8 (https://wikidocs.adobe.com/wiki/display/coldfusionen/cfinput#cfinput-History). That said: that doesn't mean one should use it. Zane, don't use the CFML UI widgets. They are poorly implemented using obsolete technology. You should just use one of the myriad markup/js/css solutions out there, eg: https://github.com/cfjedimaster/ColdFusion-UI-the-Right-Way/blob/master/chapters/cfcalendar/index.md – Adam Cameron Oct 15 '14 at 05:39
  • 1
    @AdamCameron - Yep. The part I was not sure about is whether you could use "mask" with an html "datefield". The last time I used it, it only worked with flash forms. ZaneZ - If that is still the case (extremely likely) then there is no "fix" other than using something else. Honestly, that is a good idea anyway for the reasons mentioned here and on your other thread: The UI stuff uses outdated libraries and is difficult to customize. Case in point, this thread. – Leigh Oct 15 '14 at 13:24
  • @AdamCameron you are correct but it is the combination of using the `mask` attribute with the `datefield` type that is the issue. I actually found a relevant blurb in the page that you referenced. It mentions that for the `mask` attribute _In HTML format, it does not prevent users from typing a date that does not follow the mask into a datefield input control._ Kind of pointless huh! I will add this to my answer as well. – Miguel-F Oct 15 '14 at 14:08
  • Aha, OK. That bites. Unsurprising from the CFUI stuff though. – Adam Cameron Oct 15 '14 at 14:24

1 Answers1

1

I am going to go ahead and add an answer here since another question has been opened regarding the same issue. I believe the problem is that the mask attribute on the <cfinput type="datefield" ... code only works when using Flash forms - documentation reference.

I have emphasized the text from that documentation below:

Masking cfcalendar and datefield input

In the cfcalendar tag and the Flash format datefield input control, you use the following masks to determine the format of the output. You can use uppercase or lowercase characters in the mask:

...

The following pattern specifies that the Flash form sends the date selected using a datefield input control to ColdFusion as text in the format 04/29/2004:

<cfinput name="startDate" type="datefield" label="date:" mask="mm/dd/yyyy"/>

Since you are not using a Flash form the mask is not working for you. You could try switching to a regular <cfinput type="text" ... input and change your mask to something like "99/99/9999". That would give you the correct format but the user could still enter invalid dates so you would need additional code to catch that.

In the comments you stated:

What is strange is that I have others that actually work like.. <cfinput type="text" name="odate" id="odate" validateat="onSubmit" validate="noblanks" required="yes" message="Please enter odometer date." value="" mask="99/99/9999" placeholder="08/05/2014"> but for some reason it is just the datefield that will not except the MASK

Notice that you are using a "text" input here so the mask works (as in my previous comment). It is only with the "datefield" type that the mask does not work; unless you are using a Flash form.

This is just another example of why using the built-in ColdFusion UI tags is not a good idea. They work for very simple examples but when you need more customization they fail you. You would be better off to use a JavaScript library (like jQuery) for client side validation. Adobe's own Ben Forta acknowledged this several years ago. And the ColdFusion-UI-the-Right-Way project was started because of this as well.

EDIT

Adam pointed out another reference in the ColdFusion documentation that reinforces my point. I have emphasized the text from that documentation below:

Masking input data

In HTML and Flash forms, the mask attribute controls the format of data that can be entered into a text field or that is selected in a datefield input control calendar. In HTML format, it does not prevent users from typing a date that does not follow the mask into a datefield input control. You can combine masking and validation on a field.

Community
  • 1
  • 1
Miguel-F
  • 13,450
  • 6
  • 38
  • 63