本文介绍了我需要从PHP中的当前日期开始将年龄限制在18岁以下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用javascript或ajax,从PHP中的当前日期开始将年龄限制在18岁以下.我该怎么办?

I need to restrict age for below 18 years of age from the current date in Php using javascript or ajax. How can I do this?

请检查我的代码,我要计算onblur或onSubmit的年龄,请仔细检查代码.

Please check my code I want to calculate the age onblur or onSubmit Please go through the code.

function getAge(dateString) {
  var today = new Date();
  var birthDate = new Date(dateString);
  var age = today.getFullYear() - birthDate.getFullYear();
  var m = today.getMonth() - birthDate.getMonth();
  var da = today.getDate() - birthDate.getDate();
  if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
    age--;
  }
  if (m < 0) {
    m += 12;
  }
  if (da < 0) {
    da += 30;
  }
  return age;
}
var age = getAge("1987/08/31");
alert(age);
if (age < 18) {
  alert("This age is restrict");

} else {

  alert("This age is allowed");
}
<form>
  <input type="text" id="dob" onBlur="function getAge(dateString)" />
</form>

推荐答案

尝试一下..

 <script>
function getAge() {
var dateString = document.getElementById("date").value;
if(dateString !="")
{
    var today = new Date();
    var birthDate = new Date(dateString);
    var age = today.getFullYear() - birthDate.getFullYear();
    var m = today.getMonth() - birthDate.getMonth();
    var da = today.getDate() - birthDate.getDate();
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
        age--;
    }
    if(m<0){
        m +=12;
    }
    if(da<0){
        da +=30;
    }

  if(age < 18 || age > 100)
{
alert("Age "+age+" is restrict");

} else {

alert("Age "+age+" is allowed");
}
} else {
alert("please provide your date of birth");
}
}


</script>
<input type="text" id="date" value="1987/08/31" onblur="getAge()">

http://js.do/code/65643

这篇关于我需要从PHP中的当前日期开始将年龄限制在18岁以下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 12:23