validation format

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kingdm
Forum Commoner
Posts: 27
Joined: Thu Dec 03, 2009 9:32 am

validation format

Post by kingdm »

Hello.

I'm wondering of the solution for a validation regarding this sequence.

MM/DD/YYYY - MM/DD/YYYY

How can I validate if the user input that sequence?

Thanks.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: validation format

Post by AbraCadaver »

This will do it mostly, except there is no way to determine whether they entered MM/DD/YYYY versus DD/MM/YYYY. You'd be better off implementing a date picker, either Javascript or a simple one with dropdown boxes.

Code: Select all

$str = '02/20/1971 - 02/20/2010';
 
if(!preg_match('#[\d]{2}/[\d]{2}/[\d]{4} - [\d]{2}/[\d]{2}/[\d]{4}#')) {
    //error
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
kingdm
Forum Commoner
Posts: 27
Joined: Thu Dec 03, 2009 9:32 am

Re: validation format

Post by kingdm »

Thanks for your input AbraCadaver. This is just what I need, I'm planning to also on those options you suggest. As of now, this would fit.

Huge thanks once more.
kingdm
Forum Commoner
Posts: 27
Joined: Thu Dec 03, 2009 9:32 am

Re: validation format

Post by kingdm »

I tested your code once more, I think there is an error in here. It wont print anything even if I put a echo in the if.

Code: Select all

<?php
 
$str = '02/20/1971 - 02/20/2010';
 
if(!preg_match('#[\d]{2}/[\d]{2}/[\d]{4} - [\d]{2}/[\d]{2}/[\d]{4}#',$str)) {
    echo "Error";
}
 
?>
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: validation format

Post by AbraCadaver »

That's because it matches and the ! means if not a match. Change to this and try it:

Code: Select all

$str = '02-20-1971 - 02-20-2010';
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
kingdm
Forum Commoner
Posts: 27
Joined: Thu Dec 03, 2009 9:32 am

Re: validation format

Post by kingdm »

Thank you AbraCadaver
Post Reply