简体   繁体   中英

Php regular expression for date

something is wrong in my php regular expression which is not accepting 0 before Month and date. My date formate is MM/DD/YY . It could be 6/6/2014 or 06/06/2014.

if(!empty($datepicker)){
    if(preg_match("/^([0-9]|1[0-2])\/([0-9]|[1-2][0-9]|3[0-1])\/[0-9]{4}$/", $datepicker) !==1)
        $err[] = "Invalid date of birth";
}

Just add it as:

/^(0?[0-9]|1[0-2])\/(0?[0-9]|[1-2][0-9]|3[0-1])\/[0-9]{4}$/
// ^^                ^^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM