我需要将当前日期的年龄限制在18岁以下

时间:2015-08-18 08:41:29

标签: javascript php ajax

我需要使用java脚本或ajax将Php中当前日期的年龄限制在18岁以下。我怎么能这样做?

请检查我的代码我想计算年龄onblur或onSubmit请仔细阅读代码。

<html>
    <head>
        <title>
            DOB calculations
        </title>

        <script type="text/javascript">

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");
}


        </script>
    </head>

    <body>
        <form>
<input type="text" id="dob" onBlur="function getAge(dateString)"/>
        </form>
    </body>
</html>

4 个答案:

答案 0 :(得分:0)

试试这个..

 <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

答案 1 :(得分:0)

这是另一个如何让这个工作变得更干净的例子!

function getAge() 
{
    var dateString = document.getElementById("dob").value;
    if(dateString !="")
    {
        var today = new Date();
        var birthDate = new Date(dateString); //format is mm.dd.yyyy
        var age = today.getFullYear() - birthDate.getFullYear();

        if(age < 18 || age > 100)
        {
            alert("Age "+age+" is restrict");
        } 
        else 
        {
            alert("Age "+age+" is allowed");
        }
    } 
    else 
    {
        alert("please provide your date of birth");
    }
}
<body>
<input type="date" id="dob">
<br>

<input type="button" value="Get Age" onclick="return getAge()">

</body>

答案 2 :(得分:0)

这里是年龄验证的另一个例子 请尝试一下

&#13;
&#13;
	function getAge() 
	{
		var dateString = document.getElementById("date").value;
		alert(dateString);
		
		var today = new Date();
		var year = today.getFullYear()-18;	
		alert(year);
		var month=0;
			if(today.getMonth() < 10)
			{
				month="0"+(today.getMonth()+1);
			}
			else
			{
				month=(today.getMonth()+1);
			}
			alert(month);
			var day=0;
			if(today.getDate() < 10)
			{
				day="0"+today.getDate();
			}
			else
			{
				day=today.getDate();
			}
			alert(day);
		
		var str = year +"-"+ month +"-"+ day;
		var date1 = new Date();
			date1 = str;
			
			alert(str);
		
		if (dateString>str)
			  {
			  alert("You Are NOt Able To Join Us!");
			  }
			else
			  {
			  alert("You Are Able To Join Us!");
			  }
					
	}
</script>
&#13;
<input type="date" id="date" />

<input type="submit" id="btnSubmit" value="Submit" class="btn" name="B4" onclick="return getAge();">
&#13;
&#13;
&#13;

答案 3 :(得分:-1)

  $date = DateTime::createFromFormat('d/m/Y', $this->input->post('age'));
            $dob = $date->format('Y-m-d');
            $birthdate = new DateTime($dob);
            $today = new DateTime('today');
            $age = $birthdate->diff($today)->y;
            if ($age >= 18) { true } else { false }