Jump to content


Photo

Check User Inputed String for valid date


  • Please log in to reply
5 replies to this topic

#1 jtravis

jtravis
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 14 March 2006 - 06:11 PM

Hello all,

I'm needing a function or at least a good idea on how to check the value of a string to see if it is in a valid date format before inserting into mysql.

I've been looking at the string functions, but if there is something easier to use, I'd rather use that.

Form to take input.
<td><p align="center"><strong>Date Issued<br>
      </strong><span class="style3">(Enter '00' for the day if unknown.)</span> </p></td>
    <td> <input name="dateIssued" type="text" maxlength="10">
      <br>
    <span class="style3">(YYYY-MM-DD)</span><br></td>

Is my best option to loop through the the above string value?

TYIA

#2 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 14 March 2006 - 06:13 PM

preg_match using regular expressions.. or a JScript (also regular expressions) solution would be your best bet.. i will write something out soon to help out [=

<?php
    $date = "2006-03-12";
    $pattern = "([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})"; // 4 characters 0-9, followed only by a hyphen, 1 or 2 characters allowed, followed by a hyphen, 1 or 2 characters allowed, followed by a hyphen.

    if ( preg_match ( $date, $pattern ) ) {
        //do whatever you want here..
    } else {
        //thrown an error or something
    }
?>

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#3 jtravis

jtravis
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 14 March 2006 - 06:26 PM

Sweet!. Thanks keeB! BTW, I've implemented your idea for debugging.

#4 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 14 March 2006 - 06:33 PM

[!--quoteo(post=354982:date=Mar 14 2006, 06:26 PM:name=jtravis)--][div class=\'quotetop\']QUOTE(jtravis @ Mar 14 2006, 06:26 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Sweet!. Thanks keeB! BTW, I've implemented your idea for debugging.
[/quote]

Good to hear.. it's saved my ass a few times, that's for sure..

It's a little tricky to do in objects, because they have to be defined locally.. [=

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 14 March 2006 - 07:57 PM

This will check the y,m,d values are OK

$date = "2006-04-31";
list ($y,$m,$d) = explode('-', $date);
if (checkdate($m,$d,$y))
    echo 'OK';
else
    echo "No";

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 14 March 2006 - 08:35 PM

[!--quoteo(post=355022:date=Mar 14 2006, 07:57 PM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ Mar 14 2006, 07:57 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
This will check the y,m,d values are OK

$date = "2006-04-31";
list ($y,$m,$d) = explode('-', $date);
if (checkdate($m,$d,$y))
    echo 'OK';
else
    echo "No";
[/quote]

You and your list() function!! It's really cool, and I haven't used it yet.

With the preg_match entered up there I don't think it's possible to enter an 'invalid' date, but this is cool.

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users