truelogicINC
truelogicINC

Reputation: 119

Jquery Select Features auto age on Birthday instead of Input Type Date

How can I make auto compute age when the user select month, day and year. I have a code for wherein it auto compute the age. But I want to have instead of input. Please help me to fix this :(

Here's my input type="date"

<input type="text" class="form-control" placeholder="Age" disabled id="age" name="age">

Here's my Jquery

 $("#dateofbirth").change(function(){
            var today = new Date();
            var dob = new Date($("#dateofbirth").val());
            var age = new Date(today - dob).getFullYear() -1970;
            $("#age").val(age);

});

Here's the JSFIDDLE Working For Input Type="date"

Please help me to convert it to select like this below

<select class="form-control" id="dob-month">
    <option value="0">Month</option>
    <option value="January">January</option>
    ...
</select>

<select class="form-control" id="dob-day">
    <option value="0">Day</option>
    <option value="1">1</option>
    ...
</select>

<select name="b2byear" class="form-control col-sm-3" id="dob-year">
    <option value="0">Year</option>
    <option value="2004">2004</option>
    <option value="2003">2003</option>
    ...
</select>

And it will auto compute in here:

 <input type="text" class="form-control" placeholder="Age" disabled id="age" name="age">

Upvotes: 2

Views: 1664

Answers (2)

Guruprasad J Rao
Guruprasad J Rao

Reputation: 29683

Here you go with DEMO. I've added a class for your each select. See inline comments for more detailed explaination!

$('.year,.month,.day').on('change',function(){ //call change function on all 3 select dropdowns
    var selectedYear=$('.year').find('option:selected').val();//get selected year
    var selectedDay=$('.day').find('option:selected').val();//get selected day
    var selectedMonth=$('.month').find('option:selected').val(); //get selected month
    if(selectedYear!=0 && selectedMonth!=0 && selectedDay!=0) //check if all the 3 dropdown values are selected
    {
        var today = new Date();
        var dob=new Date(selectedDay+"/"+selectedMonth+"/"+selectedYear); //convert to a valid date
        var age = new Date(today - dob).getFullYear() -1970; //calculate age
        $("#age").val(age);//display it
    }
})

UPDATE

Few Changes with DEMO:

$('.year,.month,.day').on('change',function(){
    var selectedYear=$('.year').find('option:selected').val();
    var selectedDay=$('.day').find('option:selected').val();
    var selectedMonth=$('.month').find('option:selected').val();
    if(selectedYear!=0 && selectedMonth!=0 && selectedDay!=0)
    {
        var today = new Date();
        var dob=new Date(selectedDay+"/"+selectedMonth+"/"+selectedYear);
        var age = today.getFullYear() - dob.getFullYear();//just get it directly here
        $("#age").val(age);
    }
})

UPDATE 2 with DEMO

$('.year,.month,.day').on('change',function(){

    var selectedYear=$('.year').find('option:selected').val();
    var selectedDay=$('.day').find('option:selected').val();
    var selectedMonth=$('.month').find('option:selected').val();
    if(selectedYear!=0 && selectedMonth!=0 && selectedDay!=0)
    {
        var today = new Date();
        var dob=new Date(selectedYear,(selectedMonth-1),selectedDay); //convert to date in this format
        var age = today.getFullYear() - dob.getFullYear();//just get it directly here
        $("#age").val(age);
    }

})

UPDATE 3 with DEMO

The HTML standard for forms appears to be such that disabled input elements do not contribute to the form name/value collection.

So you won't get to capture the value of disabled field and I would suggest, instead of making input field disabled make it readonly and add a class to it to make it feel disabled with some styles added to it as below:

CSS

.readoly{
    color:darkgray;
    background-color: rgb(235, 235, 228);
    border:1px solid !important;
}

Upvotes: 3

Vishnu Prasad
Vishnu Prasad

Reputation: 727

Try this DEMO FIDDLE

 $('#dob-day,#dob-year,#dob-month').on('change',function(){

    var today = new Date();
    var dob=new Date($('#dob-month').val()+"/"+$('#dob-day').val()+"/"+                $('#dob-year').val());
    console.log(dob);
    var age = new Date(today - dob).getFullYear() -1970;
    $("#age").val(age);


        });

Upvotes: 1

Related Questions