Php regex date validation

后端 未结 4 990
天命终不由人
天命终不由人 2020-12-12 01:03

I\'m trying to validate a date input by the use of regex.

if(!preg_match(\"/^[0-9]{4}\\/[0-9]{2}\\/[0-9]{2}$/\", $_POST[\'variant\'][\'sales_start\'])) { 
           


        
4条回答
  •  一整个雨季
    2020-12-12 01:55

    You're separating the date with dashes and the regex is looking for slashes?

    Try

    if ( !preg_match( "/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/", $_POST['variant']['sales_start'] ) )
    { 
        echo "invalid";
    }
    

提交回复
热议问题