Jump to content

Proper Date Checking on a form


TapeGun007

Recommended Posts

I'm just curious if there's a much better way to do this:

 

I have a form and the input looks like this:

 

<td><input name="InspectionExp" value="<?=$InspectionExp; ?>" id="date4" placeholder="mm-dd-yyyy" /></td>

 

The id on the form is for a jquery that auto formats the input field when selected to "__-__-____".

 

By default a new user InspectionExp is a DATE field in mySQL and is set to NULL.

 

So when the form is initially loaded up, the INPUT field above is blank with the place holder showing.

 

So first I check

 

 

if(isset($_POST['LicenseExp']) {
 

 

Then for whatever reason this seems to be the only thing that works right, but seems really sloppy to me:

 

 

if(!strlen($LicenseExp) == 10){ $LicenseExp = DateTime::createFromFormat('m-d-Y', $_POST['LicenseExp'])->format('Y-m-d'); }

 

If I don't have an IF statement, then by default it's going to UPDATE the database with a 1969-12-31 date on new users.  I can't use isset because the form makes the value set (again only on new users).

 

I mean, the code works yes, but I am pretty certain one of you gurus could show me a better way.

 

 

Link to comment
Share on other sites

Whoops... I thought it worked. but when submitting the form with nothing in the INPUT field it crashes on the

if(!strlen($PostLicenseExp) == 10){ echo "yes"; $PostLicenseExp = DateTime::createFromFormat('m-d-Y', $_POST['LicenseExp'])->format('Y-m-d'); }

I see... I didn't need the ! before strlen...I had it the wrong way around.

Edited by TapeGun007
Link to comment
Share on other sites

So is the field required or not? If it is required, then it should actually say that.

 

You need three checks:

  • If the parameter is present at all. isset() does this.
  • If the date is formally valid. The DateTime::createFromFormat() method does this, but you must check the return value. Don't assume that your jQuery stuff will prevent all input errors.
  • If the date is plausible.

In any case, I strongly recommend you use a date picker instead of making the user enter a bunch of digits. It will greatly increase usability und reduce the amount of garbage input.

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.