I do not see a priori anything wrong with your function, except for some confusion maybe.
The variable "introduced":
var introducida = document.getElementById('inputBirthDate').value;
you have declared it after the variable "fIntroducida", which takes (needs) the value of the other. However, keep in mind that in order to obtain a valid date type object, unless you give it a format of type DD-MM-YYYY or another, the appropriate format must be YYYY-MM-DD (look at the date that I've given as an example).
And you only have one more case that you have not covered with else's. For the rest, everything ok.
Take a look at the example.
I hope I helped you.
Greetings.
validarFecha();
function validarFecha(){
var ahora = new Date();
var fIntroducida = new Date('2000-12-24');
//var introducida = document.getElementById('inputBirthDate').value;
var mesActual = ahora.getMonth()+1;
var diaActual = ahora.getDate();
var anoActual = ahora.getFullYear();
var mesFor = fIntroducida.getMonth()+1;
var diaFor = fIntroducida.getDate();
var anoFor = fIntroducida.getFullYear();
if (anoActual - anoFor >=18){
if(mesActual >= mesFor){
if(diaActual >= diaFor){
document.write('mayor');
}
else{
document.write('menor');
}
}
else{
document.write('menor');
}
} else {
document.write('menor');
}
}
Or with several examples today (11/24/2018):
var fechas_ejemplo = ['2000-11-23','2000-11-24','2000-11-25'];
for(var i=0;i<fechas_ejemplo.length;i++){
document.write('fecha'+(i+1)+': '+fechas_ejemplo[i]+' Resultado: ');
validarFecha(fechas_ejemplo[i]);
document.write(' | ');
}
function validarFecha(fecha){
var ahora = new Date();
var fIntroducida = new Date(fecha);
var mesActual = ahora.getMonth()+1;
var diaActual = ahora.getDate();
var anoActual = ahora.getFullYear();
var mesFor = fIntroducida.getMonth()+1;
var diaFor = fIntroducida.getDate();
var anoFor = fIntroducida.getFullYear();
if (anoActual - anoFor >=18){
if(mesActual >= mesFor){
if(diaActual >= diaFor){
document.write('mayor');
}
else{
document.write('menor');
}
}
else{
document.write('menor');
}
} else {
document.write('menor');
}
}
To put it in the appropriate format, if for example your date has the format dd / mm / yyyy:
var fecha_inicial = "24/11/2000";
var fecha_array = fecha_inicial.split("/"); //la conviertes en un array
var dateObject = new Date(fecha_array[2], fecha_array[1] - 1, fecha_array[0]); //la reordenas y la conviertes en un objeto date válido