homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

validate yyyy-mm-dd

5+ Year Member

Msg#: 3744781 posted 1:36 am on Sep 15, 2008 (gmt 0)

I'm storing dates in a mysql database (varchar field). When someone edits a date how can I validate that the format is yyyy-mm-dd?

<form action="index.php?id=5" method="post">
Date: <input type="text" name="date" id="date" value="<?php echo $water["date"]; ?>" /><br/>
Alkalinity: <input type="text" name="alkalinity" id="alkalinity" value="<?php echo $water["alkalinity"]; ?>" /><br/>
<input type="submit" name="submit_edit" value="Submit" />



WebmasterWorld Senior Member 5+ Year Member

Msg#: 3744781 posted 6:11 pm on Sep 15, 2008 (gmt 0)

You really need to validate on the backend. Javascript is not 100% reliable as some browsers don't fully support it, and some users disable it.

In a matter of life and death like this, you need to be sure so validate with your own server script.


WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member

Msg#: 3744781 posted 6:29 pm on Sep 15, 2008 (gmt 0)

I agree with Dabrowski. If the validation is crucial, then perform it on the server side. Once you have that in place you can certainly enhance your page to also perform client side validation, but make sure you're not relying on JavaScript alone.

I would think you could use a RegExp to validate the date. Something like:


I think that would work, but I haven't tested it. In that example, myString would contain the date value you wanted to test and it would return true if the value matched yyyy-mm-dd. However, that example doesn't validate that mm isn't greater than 12, etc. In other words, this would still validate:

But maybe that will get you started. :)


WebmasterWorld Senior Member 5+ Year Member

Msg#: 3744781 posted 12:52 pm on Sep 16, 2008 (gmt 0)

To properly validate with Javascript, you'd first have to validate the string as Fotiman suggests.

Of course, that's if the used a '-' as a separator! What if they entered yyyy/mm/dd?

I prefer to use this, that way the user can enter either and it'll still work. This will change '/' to '-' first:
myString.replace( /\//, "-");
if( !(myString.match(/^\d{4}-\d{2}-\d{2}$/)) {
// return with error

Then you'd have to split the string:
myYMD = myString.split( "-");

An easy way of checking if the date is valid is trying to set the date you're given, then seeing if it's the same. If the date is invalid the Date function will 'wrap' it to the correct day/month. e.g., if you try to set the 40th of Jan, it will wrap to 9th Feb.

// Bear in mind that months start at 0
myDate = new Date( myYMD[0], myYMD[1], myYMD[2]);
if( myDate.getFullYear() != myYMD[0]
&& myDate.getMonth() != myYMD[1]
&& myDate.getDate() != myYMD[2]) {
// date entered was invalid!

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved