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?
Asked
Active
Viewed 3,115 times
2

Fish Below the Ice
- 1,273
- 13
- 23

Sathish Chelladurai
- 670
- 1
- 8
- 23
-
How are they entering the date and how are you verifying (just server side or client side too)? You will probably have to verify the first 4 characters are numeric. – Matt Busche Jul 18 '14 at 11:58
-
2You can accept date in any format, validated on client side, and the can convert it to desired format while processing on the server. – CFML_Developer Jul 18 '14 at 12:06
-
Are you asking how to ensure the user enters the date in the required format, or are you asking how to manage dates that might be entered in several different formats? – Fish Below the Ice Jul 18 '14 at 15:58
-
@FishBelowtheIce I am asking first one. How to ensure the user enters the date in the required format? – Sathish Chelladurai Jul 18 '14 at 16:34
-
I've tried to clarify your question. Please do [edit] it if it's still not quite right. – Fish Below the Ice Jul 18 '14 at 19:40
3 Answers
5
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>

Marikkani Chelladurai
- 1,430
- 1
- 13
- 23
-
1Thank you so much. Yes. I need to validate the dateformat on server side. – Sathish Chelladurai Jul 19 '14 at 09:03
4
As I said in comment, you can validate it on client side with following function
function validateDate(){
var dt = document.forms["Form_Name"]["Date_Field"].value;
var pattern =/^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})$/;
if (dt == null || dt == "" || !pattern.test(dt))
{
alert("invalid date");
return false;
}
else{
return true
}
}
then in cf code, while processing
<cfset desiredFormat=DateFormat(form.Date_Field,"yyyy-mm-dd")>
ps: the js function was taken from Javascript Date Validation ( DD/MM/YYYY) & Age Checking

Community
- 1
- 1

CFML_Developer
- 1,565
- 7
- 18
-
Thanks. Is this Solve server side validation too? Need to do the server side validation. – Sathish Chelladurai Jul 18 '14 at 13:54
-
On server, you can check with isDate(form.Date_Field) whether this is valid date or not and then can convert into your desired format with DateFormat function. – CFML_Developer Jul 18 '14 at 14:02
-
4Be careful with using isDate(). ColdFusion is pretty generous about what it considers a valid date. – Dan Bracuk Jul 18 '14 at 14:17
1
You could do this:
<cfinput name="StartDate"
validate="date"
type="text"
maxlength="10"
mask="9999-99-99"
message="Start Date must be a valid date."
required="yes">
The key is the mask attribute. However, if you pre-populate this field, attempting to change the value can become frustrating.

Dan Bracuk
- 20,699
- 4
- 26
- 43
-
I would recommend the [jquery masked input plugin](http://digitalbush.com/projects/masked-input-plugin/) before using `cfinput`. stay away from the CFUI stuff. – Sean Coyne Jul 18 '14 at 18:30