How to validate a date from user input? | Sololearn: Learn to code for FREE!

+1

How to validate a date from user input?

I want to validate the birthdate from user input. I'm using HTML 5 and Internet Explorer 8. I want to ensure that the date is in mm/dd/yyyy format and not more than the present date.

php

4/19/2018 6:12:26 AM

Ynelle Kyle Novicio

4 Answers

New Answer

+2

Is there a requirement to use IE8? It does not have great support for HTML5. Chrome, Firefox or Microsoft Edge are what you should be using. That said. <input type="text" name="date" placeholder="mm/dd/yyyy" pattern="(0?[1-9]|1[012])/(0?[1-9]|[12][0-9]|3[01])/\d{4}" required /> The html5 will do the pattern matching on the input and get you mm/dd/yyyy when you submit the form to php have the following: $todayTime = mktime(0, 0, 0, date('m'), date('d'), date('Y')); $formTime = strtotime($_POST['date']); if ($formTime > $todayTime) { // error because submitted date greater than today }

+1

$arr = explode(”/”, $date); if(checkdate($arr[0], $arr[1], $arr[2])) { /*True*/ }

0

split month, date and day to array and use checkdate function

0

yeah, thanks guys I already figure it out... but thanks for the suggestions and advices.... For now I just only have IE8 and no chrome installed in my Uncle's laptop.