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
https://forums.phpfreaks.com/topic/96631-php-date-validation/
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
https://forums.phpfreaks.com/topic/96631-php-date-validation/#findComment-494538
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.