Jump to content

PHP date validation


nealios

Recommended Posts

Hello,

 

How can i validate a date field to ensure that you can only enter a date into mmddyyyy format standard to mysql date data type?

 

Ive been validating other fields in the style below then outputting the message on back on my form page. Can anyone help me sort the date validation out?

 

 

if (!is_numeric($telephone)) 
{
	$message7 = "Please ensure numeric";
	header("Location: newcustomer.php?message7=$message7");
	exit();
}


if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
 {
	$message8 = "Please email is in the right format";
	header("Location: newcustomer.php?message8=$message8");
	exit();
}

Link to comment
Share on other sites

I like how you have a regex for an email, but can't figure out the simple date one ;)

 

I would go with this (read this article if you want some insight):

 

<?php
if (!eregi('^(0[1-9]|1[012])([012][1-9]|3[01])([012][0-9]{3})$', $date)) {
// set error message
}
?>

 

I haven't tested it, but hope it works. I set the year part to range between 0000 and 2999, just ask if you want it differently.

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.