gpt4 book ai didi

javascript - 如何从 JavaScript 中的下拉列表中验证 DOB?

转载 作者:行者123 更新时间:2023-12-02 23:56:02 26 4
gpt4 key购买 nike

我正在创建一个 html 表单,该表单应使用 Javascript 进行验证。注册时,用户的年龄应大于 16 岁,我从下拉列表中选择 DOB。现在,我的问题是如何使用 javscript 验证 DOB 并且用户的年龄不应大于 16

我已经验证了 DOB,正如您在代码中看到的那样,但这不是正确的验证方式

<head>
<script type="text/javascript">

function myFunction() {
var fname = document.getElementById("fname").value;
var lname = document.getElementById("lname").value;
var email = document.getElementById("email").value;
var remail = document.getElementById("remail").value;
var password = document.getElementById("password").value;


var status = false;
var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;


var x = document.getElementById("male").checked;
var y = document.getElementById("female").checked;

var year1 = document.getElementById("year").value;
var month1 = document.getElementById("month").value;
var day1 = document.getElementById("day").value;



if(fname=="")
{
alert("Please Enter First Name");
}

else if(lname=="")
{
alert("Please Enter Last Name");
}
else if(email=="")
{
alert("Please Enter Email");
}
else if (document.myform.email.value.search(emailRegEx) == -1)
{
alert("Please enter a valid email address.");
}
else if(remail=="")
{
alert("Please Enter Re-enter Email");
}

else if(email != remail)
{
alert("Please Check Email");
}
else if(password=="")
{
alert("Please Enter Password");
}
else if (password.length<8) {
alert("Please enter 8 digit password");
}
else if(day1==0)
{
alert("Please select Day")
}
else if(month1==0)
{
alert("Please select Month")
}
else if(year1==0)
{
alert("Please select year")
}
else if(year1>2003)
{
alert("You are not eligible. Age should be above 16...!!!")
}

else if(x!=true && y!=true)
{
alert("gender");
}
else
{

alert("Name = " + fname + "\n" + "Last Name = " + lname
+ "\n" + "Email = " + email
+ "\n" + "Password = " + password
+ "\n" + "DOB = " + day1 + "-" + month1 + "-" + year1
);
}

}

</script>
</head>

<body>

<form name="myform">
<h1>User Registration Form</h1>
<input type="text" placeholder="First Name" id="fname">
<input type="text" placeholder="Last Name" id="lname"><br><br>
<input type="text" name="email" placeholder="Email" id="email"><br><br>
<input type="text" placeholder="Re-enter Email" id="remail"><br><br>
<input type="password" placeholder="Enter Password" id="password"><br><br>

Birthdate : <br>
<select id="day">
<option value="0">day</option>
<option value="1">1</option>
<option value="2">2</option>
......

<option value="31">31</option>
</select>


<select id="month">
<option value="0">Month</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>......
<option value="12">12</option>
</select>


<select id="year">
<option value="0">Year</option>
<option value="1996">1996</option>
<option value="1997">1997</option>
<option value="1998">1998</option>
<option value="1999">1999</option>......
<option value="2019">2019</option>
</select>

男性 女

注册

最佳答案

请尝试一下,

NOTE : Try to use a date picker for date selection

function validateDOB(){
if(document.getElementById('dob').value==''){
alert('Please select a date')
return false
}
var dob=document.getElementById('dob').value
console.log(dob)
var today = new Date();
var birthDate = new Date(dob);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
if(age<16){
alert('You are not eligible. Age should be above 16...!!!')
}
}
<input type='date' id='dob'>
<button onclick='validateDOB()'>validate</button>

关于javascript - 如何从 JavaScript 中的下拉列表中验证 DOB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55389890/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com