Page 1 of 1

validation format

Posted: Sat Jan 02, 2010 10:27 am
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.

Re: validation format

Posted: Sat Jan 02, 2010 11:21 am
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
}

Re: validation format

Posted: Sat Jan 02, 2010 12:05 pm
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.

Re: validation format

Posted: Sat Jan 02, 2010 2:04 pm
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";
}
 
?>

Re: validation format

Posted: Sat Jan 02, 2010 2:33 pm
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';

Re: validation format

Posted: Sun Jan 03, 2010 12:17 am
by kingdm
Thank you AbraCadaver