Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php regex date validation

Tags:

regex

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'])) { 
  echo "invalid";
}

The string I'm trying to input is 2011-02-03, however it's failing, and I can't seem to figure out why.

Can someone please tell me what I'm doing wrong?

Thanks in advance

like image 460
Kristian Avatar asked Apr 07 '26 07:04

Kristian


1 Answers

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";
}
like image 134
Kalessin Avatar answered Apr 09 '26 00:04

Kalessin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!