/    Sign up×
Community /Pin to ProfileBookmark

How JS validate YYYY-MM-DD format?

Hi

I want to have a js to validate the YYYY-MM-DD format. Something like

[code]
var fi = document.priceform;

if((fi.price.value==””)||isNaN(fi.price.value))
{
window.alert(‘Please input valid price’);
fi.price.focus();
return false;
}
[/code]

But, change it to valid the date format.

If user input 06-13-2007, 30-08-2006, 2005-12-28, 23/12/2001, 2008/07/16 etc all return false.

How can I do it?

Thanks for help

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@KorJul 17.2008 — It is impossible. You will never know whether 2008-03-04 wanted to be, in the user's mind, the 3rd of April or the 4th of March. Both are valid dates. Usually this kind of indetermination is solved by using 3 list boxes (or fields): Year, Month, Day. As a general rule, based on Murphy's Laws: Don't give the user not even the slightest chance to input a wrong date because he will do it for sure.
Copy linkTweet thisAlerts:
@FangJul 17.2008 — The best way is to use 3 dropdowns (the month is alphabetic), that way you only have to make allowance or check for a leap year.

Kor your too quick
Copy linkTweet thisAlerts:
@yearbassJul 17.2008 — [CODE]<script type="text/javascript">
function check_date (d)
{
var strMistakes="";

var datePattern = /^d{4}-d{2}-d{2}$/gi;
var testDate = true;
if (String(d).match(datePattern) == null) testDate = false;

if (testDate)
{
var theDate, theMonth, theYear

theDate = parseInt(d.substr(8,2));
theMonth = parseInt(d.substr(5,2));
theYear = parseInt(d.substr(0,4));

if (theMonth < 0 || theMonth > 12) strMistakes = "Month is invalidn";
if (theDate < 0 || theDate > 31) strMistakes = "Date is invalidn";

if (theDate >= 1 && theDate <= 31)
{
if (theMonth == 2)
{
if (theYear % 4 == 0)
{
if (theDate < 0 || theDate > 29) strMistakes = "Date is invalidn";
}
else
{
if (theDate < 0 || theDate > 28) strMistakes = "Date is invalidn";
}
}
}
}
else
{
strMistakes = "Date format is invalidn";
}
return strMistakes;
}

function checkDate(d){
var valid_date = check_date(d);
if (valid_date == "") {
alert("Valid Date");
} else {
alert(valid_date)
}
}
</script>
<input type="text" name="date" id="date"/>
<input type="button" value="Check Date" onclick="checkDate(document.getElementById('date').value)"/>[/CODE]
Copy linkTweet thisAlerts:
@KorJul 17.2008 — [B]yearbass[/B], you are too optimistic. Besides that, your way to detect the leap years is not accurate. There are years, divisible by 4, which are not leap years, for instance years which are divisible by 100 but [I]not[/I] by 400 (i.e.:2100).

To check whether a date is valid or not ([I]when[/I] the separation month/day is firm) is to be used the internal processor's calendar, with the help of [B]Date()[/B] object (in javascript) and its properties. But this could be another topic.
Copy linkTweet thisAlerts:
@yearbassJul 17.2008 — yearbass, you are too optimistic. Besides that, your way to detect the leap years is not accurate. There are years, divisible by 4, which are not leap years, for instance years which are divisible by 100 but not by 400 (i.e.:2100).

To check whether a date is valid or not (when the separation month/day is firm) is to be used the internal processor's calendar, with the help of Date() object (in javascript) and its properties. But this could be another topic.[/QUOTE]


great, you're right ?

but how about this:

[CODE]<script type="text/javascript">
function check_date (d)
{
var strMistakes="";

var datePattern = /^d{4}-d{2}-d{2}$/gi;
var testDate = true;
if (String(d).match(datePattern) == null) testDate = false;

if (testDate)
{
var theDate, theMonth, theYear
theDate = parseInt(d.substr(8,2));
theMonth = parseInt(d.substr(5,2));
theYear = parseInt(d.substr(0,4));
if (theMonth < 0 || theMonth > 12) strMistakes = "Month is invalidn";
theMonth = theMonth-1;
var validDate = new Date(theYear,theMonth,theDate);
if (validDate.getFullYear() != theYear ||
validDate.getMonth() != theMonth ||
validDate.getDate() != theDate) {
strMistakes += "Date is invalidn";

}

}
else
{
strMistakes = "Date format is invalidn";
}

return strMistakes;
}

function checkDate(d){
var valid_date = check_date(d);
if (valid_date == "") {
alert("Valid Date");
} else {
alert(valid_date)
}
}
</script>
<input type="text" name="date" id="date"/>
<input type="button" value="Check Date" onclick="checkDate(document.getElementById('date').value)"/>[/CODE]
Copy linkTweet thisAlerts:
@KorJul 17.2008 — Much too intricate. Supposing the format is [I]already[/I] a correct YYYY-MM-DD (but, as I have said, you can not validate for [I]that[/I]), you may test if the date is valid like that:
<i>
</i>function validDate(date){
var mes=['Date is valid','Invalid date!'];
var d=date.split('-');
var D=[];
var DT=new Date(d[0]+'/'+d[1]+'/'+d[2]);//construct
D[0]=DT.getFullYear();D[1]=DT.getMonth()+1;D[2]=DT.getDate();//de-construct
for(var i=0;i&lt;d.length;i++){
if(d[i]!=D[i]){alert(mes[1]);return}
}
alert(mes[0]);
}


The problem remains the same: you will never be sure if the user enters YYYY-MM-DD or YYYY-DD-MM
Copy linkTweet thisAlerts:
@yearbassJul 17.2008 — Much too intricate. Supposing the format is [I]already[/I] a correct YYYY-MM-DD (but, as I have said, you can not validate for [I]that[/I]), you may test if the date is valid like that:
<i>
</i>function validDate(date){
var mes=['Date is valid','Invalid date!'];
var d=date.split('-');
var D=[];
var DT=new Date(d[0]+'/'+d[1]+'/'+d[2]);//construct
D[0]=DT.getFullYear();D[1]=DT.getMonth()+1;D[2]=DT.getDate();//de-construct
for(var i=0;i&lt;d.length;i++){
if(d[i]!=D[i]){alert(mes[1]);return}
}
alert(mes[0]);
}


The problem remains the same: you will never be sure if the user enters YYYY-MM-DD or YYYY-DD-MM[/QUOTE]


cool, you made it so simple ?

actualy the important point only at here
[CODE]var validDate = new Date(theYear,theMonth,theDate);
if (validDate.getFullYear() != theYear ||
validDate.getMonth() != theMonth ||
validDate.getDate() != theDate) {
strMistakes += "Date is invalidn";

}[/CODE]

and i made it too intricate by matching with regexp at first time, too bad

thanks ?
Copy linkTweet thisAlerts:
@KorJul 17.2008 — ... and there is another trick regarding the Date() object. It accepts also string type arguments, if formatted as:

Date([COLOR="Blue"]'yyyy/mm/dd'[/COLOR])
Copy linkTweet thisAlerts:
@felgallJul 17.2008 — Why would someone enter CCYY-DD-MM?

The three date formats in common use around the world are

CCYY-MM-DD

DD-MM-CCYY

MM-DD-CCYY

so if the year is on the front then you can be reasonably sure that the month comes next.
Copy linkTweet thisAlerts:
@KorJul 18.2008 — Why would someone enter CCYY-DD-MM?[/QUOTE]
Don't rely on the user's common sense ? [I]Never[/I] give the user not even the slightest chance to input something wrong, because he will do it for sure :rolleyes: I guess Einstein once said that Humans are one of the most stupid species on the Earth, mainly because we think we are the smarter. ?
Copy linkTweet thisAlerts:
@FangJul 18.2008 — If we all used the standard validation would be easier
Copy linkTweet thisAlerts:
@KorJul 18.2008 — If we all used the standard validation would be easier[/QUOTE]
Well yes, but those are standards for programmers, not for common users ?

Think about: Over 200 years have passed since the meter and Celsius deg were introduced as standard measurement units but still the stubborn Americans use their inch/feet/mile and Fahrenheit stupid and intricate units...

===

Note: I Agree with your sig:

"At least 98% of internet users' DNA is identical to that of chimpanzees" ?
×

Success!

Help @techissue2008 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...