Okay, I'm very new to Javascript (I started about 2 months ago in my computer's class) and I'm doing an assignment that requires a form that includes date of birth (with day/month/year each in a dropdown) and I need to be able to:
a) Confirm that the user is over 18 and send an alert if not.
and;
b) Provide a "happy birthday" message if the current day is the user's birthday
I've searched tirelessly for a solution but I can't find one that I understand. This is what I've got so far:
<form name="contest">
Date of Birth:
Year: <select name="year">
<option></option>
<option>2015</option>
<option>2014</option>
<option>2013</option>
<option>2012</option>
<option>2011</option>
<option>2010</option>
<option>2009</option>
<option>2008</option>
<option>2007</option>
<option>2006</option>
<option>2005</option>
<option>2004</option>
<option>2003</option>
<option>2002</option>
<option>2001</option>
<option>2000</option>
<option>1999</option>
<option>1998</option>
<option>1997</option>
<option>1996</option>
<option>1995</option>
<option>1994</option>
<option>1993</option>
<option>1992</option>
<option>1991</option>
<option>1990</option>
</select>
Month: <select name="month">
<option></option>
<option value="jan">January</option>
<option value="feb">February</option>
<option value="mar">March</option>
<option value="apr">April</option>
<option value="may">May</option>
<option value="jun">June</option>
<option value="jul">July</option>
<option value="aug">August</option>
<option value="sep">September</option>
<option value="oct">October</option>
<option value="nov">November</option>
<option value="dec">December</option>
</select>
Day: <select name="day">
<option></option>
<option>01</option>
<option>02</option>
<option>03</option>
<option>04</option>
<option>05</option>
<option>06</option>
<option>07</option>
<option>08</option>
<option>09</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
<option>16</option>
<option>17</option>
<option>18</option>
<option>19</option>
<option>20</option>
<option>21</option>
<option>22</option>
<option>23</option>
<option>24</option>
<option>25</option>
<option>26</option>
<option>27</option>
<option>28</option>
<option>29</option>
<option>30</option>
<option>31</option>
</select>
<input type="button" name="" onClick="checkInput(this.form)" value="Submit"/>
<input type="reset"/>
</form>
Any help would be greatly appreciated! Thanks :)
Straightforward.
function checkInput()
{
// get today's date.
var today = new Date();
today.setHours(0,0,0,0); // set time to start of day for comparison.
// create a new date based on user input.
var bdate = new Date(Date.parse(
document.querySelector('select[name="year"]').value + ' ' +
document.querySelector('select[name="month"]').value + ' ' +
document.querySelector('select[name="day"]').value
));
today.setYear(0); // ignore year part of date.
bdate.setYear(0); // ignore year part of date.
if (today.valueOf() == bdate.valueOf())
{
alert('Happy Birthday');
}
else
{
alert('Merry Unbirthday.');
}
}
In your function(checkInput) you need to check if the current date is equal with the date that the user has give. You can get current date via:
var today = new Date(); var day = today.getDate(); var month = today.getMonth()+1; // +1 Because january is 0 var year = today.getFullYear();
And then its easy to make the compare.
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.