Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

107
Views
Contact Form - JQuery Age Calculation and Field Omission

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>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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()}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!