I am making a registration form and I'd like to get the birth date of each registered user.
<input class="field" type="date" name="birthday" max="9999-12-31" />
When I was making this, though, I realised that everyone can open the Inspect Element options and put any value they like. This will go to the database and just ruin the experience. I don't want to let this happen, so I am now looking for a way to do the following:
- Find a way to check if the date is valid (no matter what the format is, mm/dd/yyyy and dd/mm/yyyy will both get accepted into the database but they will be converted to yyyy-mm-dd before going there)
- Make sure the region doesn't matter (like I said, every format will get accepted but I'm a bit worried about the fact that some countries show the input field differently (For example, I stumbled across the дд/мм/гггг г. format which basically means dd/mm/yyyy y. Would that "y." at the end affect what is being stored as a variable in PHP? It just sits there, it's an abbreviation, it can't be changed by the user)
- Not let the person enter a date higher than the current one. I'm thinking of adding a simple 13-years-old age limit. How can that be done?
Before asking this question, I have been searching for a solution to all these problems for a while now. And I found out about the checkdate()
function but from what I learnt, it accepts only the mm/dd/yyyy format which is not something I want. And it also works a little bit differently. I need to input three separate values for day, month and year. So, I went to look for alternative solutions. I stumbled across this topic: Correctly determine if date string is a valid date in that format. The solution seems to be what I'm looking for. The only problem is that it only accepts the "yyyy/mm/dd" format. So, logically, I just have to find a way to detect what the format is (where my only concern is: what if the user opens Inspect Element and changes the input type... maybe the solution to this is a simple check if $_POST['birthday'] is a type="data" element first? I'm also still worried about that "г./y." which appears in some regions though). After the detection, the correct function for checking whether the date is valid or not will get iterated. After all of this, the date will be simply converted to yyyy/mm/dd and the website will make one last check to see if the user is at least 13-years-old and allowed to be registered. I don't get to work with PHP very often, so any kind of help is appreciated!