Login Retreive lost passwordRegister
Search

Forum Index / Feedback / Date Validator for text field

[ This topic is solved ]

  Moraitopoulos Apostolos 04 March 10 / 12:13  
I needed a datefield like 'dd/mm/yyyy' or so with textfield.
My solution in Class.Validator.php
<?php
 
function DateText($value)
 {
//Date d/m/yyyy and dd/mm/yyyy
//1/1/00 through 31/12/99 and 01/01/1900 through 31/12/2099
//Matches invalid dates such as February 31st
 
$regex='^\b(0?[1-9]|[12][0-9]|3[01])[- /.](0?[1-9]|1[012])[- /.](\d{4})\b^';

  if (
preg_match($regex$value)) {
    
$ar=preg_split("/[-\/.]/",$value);
    
$y = (int) $ar[2];
    
$m = (int) $ar[1];
    
$d = (int) $ar[0];
 
  
//Check months with 30 days
    
if (($m==|| $m==|| $m==|| $m==11) && $d>30) { return "error!"; }

   
//Ceck leap year 
    
if ($m == 2) {
        
$leapy = ((($y 4) == 0) && (($y 100) != 0)  || (($y 400) == 0));
        if ((
$d 29) || ($d 28 && !$leapy)) return "not a leap year!";
    }
    return 
true;
  } else {
    return 
"wrong date!";
  }
 }
?>

example:
<?php // make the datefield 
$form -> TextField("From Date1 ""datefrom",FH_DATETEXT,10,20); ?>

  Johan Wiegel (Admin) 04 March 10 / 13:35  
Nice custom validator, we will not integrate it, there are more dateformats and the once you used think about:

m/d/YY
mm/dd/YY
m/d/YYYY
mm/dd/YYYY

  Top


powered by PHP-GLOBE   © 2004 - 2010 FormHandler. All rights reserved.   -   Open source license