javascript - 使用表单输入中的值来确定它们是否超过特定年龄
问题描述
我将如何编写一个从 html 表单中获取值的程序,计算用户的年龄并确定他们是否可以访问该网站,如果他们超过了指定的年龄?
我最初的想法是在单击按钮后从输入表单中获取值。将该值与今天的日期进行比较,如果该值低于指定年龄,则用户将被引导到不同的网站。
这就是我现在所拥有的。
// get values
var userMonth = document.getElementById('userMonth');
var userDay = document.getElementById('userDay');
var userYear = document.getElementById('userYear');
var submitButton = document.getElementById('btnCheckBday');
// when the button is clicked
submitButton.addEventListener("click", calcBirthDate);
// calculating birthday
function calcBirthDate() {
// get values from user info
month = userMonth.value;
day = userDay.value;
year = userYear.value;
today = new Date();
birthday = new Date(year, month, day);
oldEnough = today - birthday;
if (oldEnough >= 21) {
console.log("You are older than 21");
} else {
console.log("You are below the age of 21");
}
}
<form>
<div class="container form-contain">
<!-- name input -->
<div class="form-group row">
<label for="name" class="col-2 col-form-label" id="label-name">Full Name</label>
<div class="col-10">
<input type="text" name="name" class="form-control" id="input-name">
</div>
</div>
<div class="from-group row">
<!-- month -->
<label for="month" class="col-2 col-form-label" id="label-year">Month:</label>
<div class="col-2">
<input type="number" name="month" class="form-control" id="userMonth">
</div>
<!-- Day -->
<label for="day" class="col-2 col-form-label" id="label-year">Day</label>
<div class="col-2">
<input type="number" name="day" class="form-control" id="userDay">
</div>
<!-- Year -->
<label for="year" class="col-2 col-form-label" id="label-year">Year</label>
<div class="col-2">
<input type="number" name="year" class="form-control" id="userYear">
</div>
<div class="form-group row">
<button class="btn btn-primary" id="btnCheckBday" value="submit">Submit</button>
</div>
</div>
</div>
</form>
解决方案
您button
被解释为提交按钮,它提交form
. 为防止这种情况,请将button
类型更改为“按钮”,或者event.preventDefault()
在用户年龄不够大时使用以防止表单提交。
此外,减去两个日期不会给出它们之间的年数,它相当于做date2.getTime()-date1.getTime()
.
// get values
var userMonth = document.getElementById('userMonth');
var userDay = document.getElementById('userDay');
var userYear = document.getElementById('userYear');
var submitButton = document.getElementById('btnCheckBday');
// when the button is clicked
submitButton.addEventListener("click", calcBirthDate);
// calculating birthday
function calcBirthDate() {
var event = arguments[0];
event.preventDefault();//call this anywhere to prevent form submission
// get values from user info
month = userMonth.value;
day = userDay.value;
year = userYear.value;
today = new Date();
birthday = new Date(year, month-1, day);
var y1 = today.getFullYear();
var m1 = today.getMonth();
var d1 = today.getDate();
var y2 = birthday.getFullYear();
var m2 = birthday.getMonth();
var d2 = birthday.getDate();
var diff = y1 - y2;
if (m2 > m1) diff--;
else {
if (m2 == m1) {
if (d2 > d1) diff--;
}
}
oldEnough = diff;
if (oldEnough >= 21) {
console.log("You are older than 21");
} else {
console.log("You are below the age of 21");
}
}
<form>
<div class="container form-contain">
<!-- name input -->
<div class="form-group row">
<label for="name" class="col-2 col-form-label" id="label-name">Full Name</label>
<div class="col-10">
<input type="text" name="name" class="form-control" id="input-name">
</div>
</div>
<div class="from-group row">
<!-- month -->
<label for="month" class="col-2 col-form-label" id="label-year">Month:</label>
<div class="col-2">
<input type="number" name="month" class="form-control" id="userMonth">
</div>
<!-- Day -->
<label for="day" class="col-2 col-form-label" id="label-year">Day</label>
<div class="col-2">
<input type="number" name="day" class="form-control" id="userDay">
</div>
<!-- Year -->
<label for="year" class="col-2 col-form-label" id="label-year">Year</label>
<div class="col-2">
<input type="number" name="year" class="form-control" id="userYear">
</div>
<div class="form-group row">
<button class="btn btn-primary" id="btnCheckBday" value="submit">Submit</button>
</div>
</div>
</div>
</form>
推荐阅读
- asp.net - 错误的“BC30205:预期语句结束。” 在运行时
- reactjs - 如何设置来自 Redux 存储的输入值,但仍然能够写入该输入并更新值?
- javascript - 使用表单中的按钮将变量附加到具有来自其他表单的变量的 url
- html - Visual Studio 代码智能感知没有看到 html 类
- loops - 如何在 Xcode10 中循环 AvAudioPlayerNode
- c# - 从表单 POST 到外部 API
- r - RDCOMClient 创建 + 写邮件到特定帐户的草稿文件夹
- visual-studio - 试图修改旧的 Windows 框架应用程序 - 如何重建?
- asp.net - 如何在 jQuery 中更改我的默认选项卡
- c# - Roslyn - 获取给定 System.Type 的 ConstructorDeclaration