JavaScript Validates Date of Birth and Block Same Year

1

I need a Javascript block that I put date with the same current year. For Field Date of birth. Can anyone help me?

For the field:

<div class="col-sm-4">
                                        <label for="dataNasc" class="control-label"> Data de Nascimento</label>
                                        <input type="date" class="form-control" name="dataNasc" id="dataNasc"/>
                                    </div>
    
asked by anonymous 09.02.2017 / 14:27

2 answers

1

Imagine you have this in HTML:

<input type="text" id="date_start">

In javascript we have this:

var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var fSDate = new Date(y, m, 1);
var fEDate = new Date(y - 1, m + 1, 0);

$.datepicker.setDefaults({
    changeMonth: false,
    changeYear: false,
    showOtherMonths: false,
    yearRange: '-0:+0',
    dateFormat: 'yy-mm-dd',
    defaultDate: +0, //30 days ago
    numberOfMonths: 1,
    minDate: fSDate, 
    maxDate: fEDate,
    showAnim: 'fadeIn',
    showButtonPanel: false
});

$('#date_start').datepicker();

Pay attention to fSDate and fEDate and notice how you can set limits for year, month, and day using a little logic. I already subtracted a year in fEDate and it will only show at most 2016.

See this online sample in JSFiddle. This example is great for you to see everything that can change until you get the desired result. Play this fiddler a little bit until you master this datepicker() .

    
09.02.2017 / 14:51
2

 var data = new Date(new Date().getFullYear() - 1, 11, 31).toISOString().slice(0,10);
document.getElementsByName("dataNasc")[0].setAttribute("max", data);
<div class="col-sm-4">
                                        <label for="dataNasc" class="control-label"> Data de Nascimento</label>
                                        <input type="date" class="form-control" name="dataNasc" id="dataNasc"/>
                                    </div>
    
09.02.2017 / 14:55