Search code examples
coldfusiondate-formatcoldfusion-9

How to validate the Dateformat Mask using ColdFusion?


How to check the date format using ColdFusion. I want to check that the user enters a date in the format yyyy-mm-dd. When a user enters a date in the format dd-mm-yyyy I want to show an error message. Is there any simple way to solve this?


Solution

  • Do you need to validate the date format from the server side? Here I've given a simple RegEx check to check the format and did some checks to validate.

    <cfset regex = '[0-2][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]'>
    <cfset myDate = '2006-12-39'>
    <cfset MatchedDate = REMatchNoCase(regex, myDate)>
    <cfif arrayLen(MatchedDate) AND isDate(myDate) AND MatchedDate[1] EQ myDate>
        Valid date
    <cfelse>
        Invalid date
    </cfif>