Date Field Mask Not Working [duplicate]

喜夏-厌秋 提交于 2019-12-12 06:53:36

问题


This is very frustrating. I am using cfinput datefield and mask="MM/DD/YYYY" and its not working. If it is cfinput text and mask="MM/DD/YYYY" it works perfect. I do not want to only have a textbox though I would like to keep the calendar that the date field gives you... The issue is when they use the calendar it is in the correct format MM/DD/YYYY but if the user just types in the date into the textbox without using the calendar the user can type whatever they desire.. (122334435) which obviously is unacceptable. Any ideas or workarounds anyone may be aware of?

Works:

    <cfform name="foo">
    <cfinput 
      type="text" 
      name="test" 
      validate="eurodate" 
      mask="99/99/9999" 
      validateat="onblur" />
    <input type="submit">
    </cfform>

Does Not Work:

    <cfform name="foo">
    <cfinput 
      type="datefield" 
      name="test" 
      validate="eurodate" 
      mask="MM/DD/YYYY" 
      validateat="onblur" />
    <input type="submit">
    </cfform> 

回答1:


I have added an answer to the other question that was originally opened regarding this same issue. I will post a bit here as well since users may find this question and not the other one. Or you could delete this question.

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.

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

On the other question that was posted 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.



来源:https://stackoverflow.com/questions/26369531/date-field-mask-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!