ColdFusion 日期字段的 JavaScript 日期格式 [英] JavaScript Date Format for ColdFusion Date Field

查看:24
本文介绍了ColdFusion 日期字段的 JavaScript 日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在此函数中添加一些设置日期格式并要求用户输入 MM/DD/YYYY 的内容?面具不工作..

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..

费用计算器功能:

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;
    }

调用函数:

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

输入按钮:(需要 ColdFusion):

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();"
       >

推荐答案

我将继续在这里添加一个答案,因为 anotherquestion 已针对同一问题提出.我认为问题在于 <cfinput type="datefield" ... 代码上的 mask 属性仅在使用 Flash 表单时有效 - 文档参考.

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:

屏蔽 cfcalendar 和 datefield 输入

在 cfcalendar 标记和 Flash 格式日期字段输入控件 中,您使用以下掩码来确定输出格式.您可以在掩码中使用大写或小写字符:

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:

...

以下模式指定 Flash 表单将使用日期字段输入控件选择的日期以 04/29/2004 格式的文本形式发送到 ColdFusion:

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"/>

由于您没有使用 Flash 表单,因此该掩码不适合您.您可以尝试切换到常规 <cfinput type="text" ... 输入并将掩码更改为 "99/99/9999" 之类的内容.这会给你正确的格式,但用户仍然可以输入无效的日期,所以你需要额外的代码来捕捉它.

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.

在你所说的评论中:

奇怪的是,我还有其他类似的工作.. <cfinput type="text" name="odate" id="odate" validateat="onSubmit" validate="noblanks" required="yes" message="请输入里程表日期."value="" mask="99/99/9999" placeholder="08/05/2014"> 但由于某种原因,它只是日期字段,除了 MASK

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

请注意,您在此处使用 "text" 输入,因此掩码有效(如我之前的评论中所述).只有 "datefield" 类型的掩码不起作用;除非您使用 Flash 表单.

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.

这只是另一个例子,说明为什么使用内置的 ColdFusion UI 标签不是一个好主意.它们适用于非常简单的示例,但是当您需要更多自定义时,它们会让您失望.您最好使用 JavaScript 库(如 jQuery)进行客户端验证.Adobe 自己的 Ben Forta几年前就承认了这一点.ColdFusion-UI-the-Right-Way 项目已启动也是因为这个.

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.

编辑

Adam 指出 ColdFusion 文档中的另一个参考这加强了我的观点.我强调了以下文档中的文字:

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

屏蔽输入数据

在 HTML 和 Flash 表单中,掩码属性控制可输入到文本字段或在日期字段输入控件日历中选择的数据格式.在 HTML 格式中,它不会阻止用户在日期字段输入控件中键入不跟随掩码的日期.您可以在字段上结合掩码和验证.

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.

这篇关于ColdFusion 日期字段的 JavaScript 日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆