Jump to content

Check User Inputed String for valid date


jtravis

Recommended Posts

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.
[code]<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>[/code]

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

TYIA
Link to comment
Share on other sites

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 [=

[code]
<?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
    }
?>
[/code]
Link to comment
Share on other sites

[!--quoteo(post=354982:date=Mar 14 2006, 06:26 PM:name=jtravis)--][div class=\'quotetop\']QUOTE(jtravis @ Mar 14 2006, 06:26 PM) [snapback]354982[/snapback][/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.. [=
Link to comment
Share on other sites

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

[code]$date = "2006-04-31";
list ($y,$m,$d) = explode('-', $date);
if (checkdate($m,$d,$y))
    echo 'OK';
else
    echo "No";[/code]
[/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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.