简体   繁体   中英

How to add birthdate (tel type datefield class) to database and set an age restriction?

As the title suggests, I am having difficulty finding a clean and elegant approach to both adding a working php function for a user's birthdate and setting an age limit to my birthdate form that I have created:

<div id="date1" class="datefield">
            <input id="day" type="tel" maxlength="2" placeholder="DD"/> /
            <input id="month" type="tel" maxlength="2" placeholder="MM"/> /
            <input id="year" type="tel" maxlength="4" placeholder="YYYY" />
</div>

Here are the variables I have set up for the birthdate:

//initializing registration variables to prevent errors
$bday = ""; //birthday
$bday = date("Y-m-d");

In my database, the birth_date type is set as just a date. Is my setup correct so far? I understand that there may be a need to separate the date into month, day, and year, but all I am interested in is the valid year in which the user entered if I want to include conditions regarding the different number of days each month has.

Edit: With the paragraph that was just replaced, I realized that I am making this more complex than necessary. All I simply would like is for a php validation function that checks whether or not an inputted year is within the desired age specifications. So, on first code you see, I will be removing both day and month fields and only keep the year field.

Off tangent(but still relevant), should I not even bother to prompt users to inputting their DOB and instead have the required age limit documented in the terms/privacy policy(ies)? I know that even with a DOB form w/ restrictions not everyone will be willing to oblige to the rules ( ex. social net services like Facebook have 13 years as age limit but kids younger can work around that by lying; Twitter has their age limit documented rather than prompted).

Much advice would be greatly appreciated, thank you.

you can use following code to validate date

ValidationExpression="(((0|1)[1-9]|2[1-9]|3[0-1])/(0[1-9]|1[0-2])/((19|20)\\d\\d))$"

Php has checkdate function to check if date is valid. And to ensure the age you can use date_add() function to create birthday datetime object + your range. Then you compare this object if it is less than current time.

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