Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Jason Shillingford
6,443 PointsForm Validating a date, client side
I want to validate a text field so that a user types exactly dd-mm-yyyy and if they try anything else then it returns false(error). Im using input type="date" so this isn't a problem on chrome or opera but it is on some browsers which display a text field instead. My php is going to handle the value as dd-mm-yyyy, so I want to keep it that way if I can. I was wondering if there was a way to do this that isnt overly complicated? I dont care much if they enter an invalid date like 55-13-5045. As long as it is in that format so my php can read it and not produce an error. here is what i have so far I just need that javascript condition.
if((/*condition*/){
$(this).removeClass("invalid").addClass("valid");
$(this).next().hide();
}else{
$(this).removeClass("valid").addClass("invalid");
$(this).next().show();
}
});
<label for="checkdate"> Date (dd-mm-yyyy)</label><br/>
<input id="checkdate" name="checkdate" type="date" value="dd-mm-yyyy" size="20"/>
<span> eg 04/08/2014 </span>
Any help is appreciated. Sorry my english is poor. Thanks in advance
7 Answers

Gordon Jiroux
8,728 PointsHopefully, I've understood what you were looking for, if not let me know I've missed the mark, and I'll try again to help you.
you can use a jQuery plug-in like the following with localization http://docs.jquery.com/Plugins/Validation/Methods/date
or
something like this Javascript: which requires "jquery.validate.min.js" be referenced
$(function() {
$('#submit').click(function() {
$.validator.addMethod(
"DateFormat",
function(value, element) {
return value.match(/^\d\d?\/\d\d?\/\d\d\d\d$/);
},
"Please enter a date in the format dd/mm/yyyy"//removed ;
);
// validate and process form here
$('#contact').validate({
rules :{//added here {
date: {
DateFormat : true
}
}//added here }
});
var dataString = $('form[name="contact"]').serialize();
$.ajax({
type: "POST",
data: dataString,
url : "ajax.php",
});
});
});
or
you could write a simple regex (regular exrpression) like:
/^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/
...in conjunction with a date sanity check to validate against so that 31-Feb-2013 doesn't validate as true...

James Barnett
39,199 PointsJason Shillingford - In my humble opinion this is a bad idea.
It would be much better to use moment.js to validate a date and it's a super easy to use plugin.

Gordon Jiroux
8,728 PointsI wish Treehouse forum answers focused more on learning how to do stuff yourself (being self sufficient) than using third party tools for everything... If you you don't know how to do it without always relying on someone else's code, how could one expect to get any better, or come up with a solution to a problem that doesn't have a 3rd party solution. I prefer my devs know the ins-and outs of any language before using someone else's code, or utilizing 3rd party libraries... Just a thought; I understand it's faster to use libraries, but without a firm grasp of what's going on behind the scenes, are you really a developer, or just an assembly line worker?
The lessons seem to seem to do a good job with teaching the ins and outs, but the forums seem to point people to various libraries... the only reason I suggested jQuery above was because it was apparent it was already in use...
To me it's no different than "Give'm a fish, or teach'm to fish"... IMHO, before using jQuery, or node, etc... a dev should know how to do without...

James Barnett
39,199 PointsGordon Jiroux - There's an old joke about regular expressions:
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
Examples of this issue include:
Dates also have edge cases that aren't accounted for in a simple regex, such as months with less than 31 days and leap years.
When Jason Shillingford mentioned
I dont care much if they enter an invalid date like 55-13-5045.
That's a huge red flag.
You should never let a user knowningly enter invalid data, that leads to a terrible user experience. The correct solution to this problem instead is to make your interface as forgiving for the user as possible, such as allowing a user in the UK to use their cultural date format and then validate the date before they submit the form and then double check for malicious stuff on the backend.

Gordon Jiroux
8,728 PointsI agree, however I think it's good to know:
how to manually validate the format
how to do a check to make sure it's valid by converting/casting the input to a date, to verify it's valid...
I just think it's good practice for devs to know what's really going on behind the scenes in these libraries... and to be able to replicate the functionality manually... that's when you "know" a language... If you visit a foreign country could you get by with "google translate'?... Yes, for the most part, but you'll be lacking in idioms an phrases...
Another example is; on all of my payment forms (where I can), I do a mod-10 check to see if a credit card number itself is "valid" before posting it to the payment gateway... no sense in sending it if you know it's going to fail (from fat finger, or finger dyslexia)... :)

James Barnett
39,199 PointsGordon Jiroux - Here's a great tutorial that covers those very topics.
http://www.sitepoint.com/beginners-guide-to-javascript-date-and-time/

Jason Shillingford
6,443 PointsWow thanks for the great response guys. Im literally going to try all of the above. I do think its important I get an understanding of how to validate from scratch, because if anything, I'll learn more about javascript programming. For final production on my site, sounds like using a library will save me a few headaches in the future.
Also I noticed some sites like Google use 3 fields for 'date', one for each day, month and year, which is interesting.
I'll post on here what I come up with by the end of today. Thanks again James Barnett and Gordon Jiroux