How To Check If A String Is A Legal "dd/mm/yyyy" Date?
Solution 1:
Edit: exact solution below
You could do something like this, but with a more accurate algorithm for day validation:
function testDate(str) {
var t = str.match(/^(\d{2})\/(\d{2})\/(\d{4})$/);
if(t === null)
return false;
var d = +t[1], m = +t[2], y = +t[3];
// Below should be a more acurate algorithm
if(m >= 1 && m <= 12 && d >= 1 && d <= 31) {
return true;
}
return false;
}
Date validation alg.: http://www.eee.hiflyers.co.uk/ProgPrac/DateValidation-algorithm.pdf
Exact solution: function that returns a parsed date or null, depending exactly on your requirements.
function parseDate(str) {
var t = str.match(/^(\d{2})\/(\d{2})\/(\d{4})$/);
if(t !== null){
var d = +t[1], m = +t[2], y = +t[3];
var date = new Date(y, m - 1, d);
if(date.getFullYear() === y && date.getMonth() === m - 1) {
return date;
}
}
return null;
}
In case you need the function to return true/false and for a yyyy/mm/dd format
function IsValidDate(pText) {
var isValid = false ;
var t = pText.match(/^(\d{4})\/(\d{2})\/(\d{2})$/);
if (t !== null) {
var y = +t[1], m = +t[2], d = +t[3];
var date = new Date(y, m - 1, d);
isValid = (date.getFullYear() === y && date.getMonth() === m - 1) ;
}
return isValid ;
}
Solution 2:
Try -
var strDate = '12/03/2011';
var dateParts = strDate.split("/");
var date = new Date(dateParts[2], (dateParts[1] - 1) ,dateParts[0]);
There's more info in this question - Parse DateTime string in JavaScript (the code in my answer is heavily influenced by linked question)
Demo - http://jsfiddle.net/xW2p8/
EDIT
Updated answer, try -
function isValidDate(strDate) {
if (strDate.length != 10) return false;
var dateParts = strDate.split("/");
var date = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);
if (date.getDate() == dateParts[0] && date.getMonth() == (dateParts[1] - 1) && date.getFullYear() == dateParts[2]) {
return true;
}
else return false;
}
This function passes all the test cases. As far as I'm aware, Adam Jurczyk had posted an accurate answer well before I corrected my original wrong answer. He deserves credit for this.
Demo - http://jsfiddle.net/2r6eX/1/
Solution 3:
you can use regular exp to validate date . try like this :
re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
if(form.mydate.value != '' && !form.mydate.value.match(re))
//do something here
note: this will only work for dd/mm/yyyy
for exact match of your requirement use
re = /^\d{2}\/\d{2}\/\d{4}$/;
Solution 4:
I'm going to answer a different question, as Misha Moroshko's has already been well-answered: use HTML5. That is, on the assumption that the strings in question arise as user inputs through a Web browser, I propose that the entries be received as
<input type = "date" ...
I recognize that not all browsers likely to be in use will interpret "date" in a way that rigorously enforces validity. It's the right thing to do, though, will certainly improve as time goes on, and might well be good enough in a particular context even now simply to eliminate the need to validate the date-string after the fact.
Solution 5:
Personally, I think the best solution would be to modify the UI to use dropdowns for the month and possibly day selections.
Trying to figure out if 1/2/2001 is January 2nd or February 1st based solely on that input string is impossible.
Post a Comment for "How To Check If A String Is A Legal "dd/mm/yyyy" Date?"