i have this idea of calculating the age from the data field of a contact form using JQuery and then use the age value to hide or show other fields of the form.
Here's a draft of the idea that i've assembled with few code snippets found on the web (sadly, i'm not used with JQuery):
<script type="text/javascript">
jQuery(document).ready(function ($) {
function getAge("#bdate") {
var today = new Date();
var birthDate = new Date("#bdate");
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
}
$(document).ready(function() {
$("#genitori").hide();
$('#bdate').change(function() {
function getAge(bdate) {
var today = new Date();
var birthDate = new Date(bdate);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
}
if (age<18) {
$("#genitori").show();
}
else {
$("#genitori").hide();
}
}
});
}
</script>
<table>
<tr>
<td>
<h3> User </h3>
</td>
</tr>
<tr>
<td>
<label for="name">Name:</label>
<input type="text*" id="name">
</td>
<td>
<label for="birthday">Birthday:</label>
<input type="date" id="bdate">
</td>
</tr>
<div id="parents">
<tr>
<td>
<h3> Parents </h3>
</td>
</tr>
<tr>
<td>
<label for="Pname">Parents Name:</label>
<input type="text*">
</td>
</tr>
</div>
</table>
IMHO, you have no idea what are you doing. But first, you make a function named getAge it bracket put a variable, name it as you want i.e. birthday; this birthday put here var birthDate = new Date(birthday); and now you can use this function on your inputs value in this way. Take a value from input like var valueOfInput = $("#dbdate").val() and put this value into function function getAge (valueOfInput) this is how you have result of counting but you want use it to if statement, best you can do it is to make a variable which hold this value; so after the function execute put var resultOfFunction or whatever you want. Now is final part you have to use this variable to if statement just like if(resultOfFunction>18){return secretField.show();}else{return secretField.hide()}