ColdFusion 日期字段的 JavaScript 日期格式

标签 javascript date coldfusion

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

费用计算器功能:

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):

<cfinput
       type="datefield"
       name="purchasedate"
       width="130"
       required="yes"
       message="Please enter purchase date."
       value="#dateformat(now(),"mm/dd/yyyy")#" 
       oninput="calculateTotal();"
       >

最佳答案

我将继续在这里添加一个答案,因为 another question已就同一问题展开。我相信问题在于mask <cfinput type="datefield" ... 上的属性代码仅在使用 Flash 表单时有效 - documentation reference .

我已经强调了以下文档中的文本:

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

由于您没有使用 Flash 表单,因此该蒙版对您不起作用。您可以尝试切换到常规<cfinput type="text" ...输入并将您的掩码更改为类似 "99/99/9999" 。这将为您提供正确的格式,但用户仍然可以输入无效日期,因此您需要额外的代码来捕获该日期。

您在评论中指出:

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"输入 mask 不起作用; 除非您使用 Flash 表单

这只是为什么使用内置 ColdFusion UI 标签不是一个好主意的另一个例子。它们适用于非常简单的示例,但当您需要更多自定义时,它们就会失败。您最好使用 JavaScript 库(如 jQuery)进行客户端验证。 Adobe自己的Ben Forta acknowledged this several years ago 。还有ColdFusion-UI-the-Right-Way project也是因为这个而开始的。

编辑

亚当指出another reference in the ColdFusion documentation这强化了我的观点。我强调了以下文档中的文本:

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.

关于ColdFusion 日期字段的 JavaScript 日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26286518/

相关文章:

coldfusion - 当名称包含连字符/破折号时从 Form 范围加载 ColdFusion 值

jquery 与 Coldfusion cfc 和复选框集成

javascript - 我可以检查 Bootstrap Modal 是否显示/隐藏吗?

php - 如何从数据库中获取特定月份的数据?

Oracle TIMESTAMP 带时区数据类型混淆

java - 如何从两个 DayOfWeeks 之间获取 DayOfWeek 的列表

java - 我可以只使用 Batik 库的转码器而不使用所有其他 Batik 代码和依赖项吗?

javascript - 如何根据列的宽度换行文本

javascript - 在不丢失缩放控制的情况下更新 amMap 中的图像 - AmCharts v4

javascript - 如何将 php 数组转换为 javascript 的 varname.arrkey 形式?