【发布时间】:2020-04-09 09:32:00
【问题描述】:
所以我正在为一家需要年龄验证的啤酒厂开发一个网站。验证有效,它会询问您的出生日期和名字。我唯一需要做的就是在网站上显示名字并打上一句问候“嘿,“你的名字”!欢迎来到我们的啤酒厂!只有你年龄在 18 到 101 岁之间。
//get birthdate
function calculateAge(dateString) {
var today = new Date();
var birthDate = new Date(dateString);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
//validate user age
const displayCheck = window.prompt(
"Please Enter your birthdate in format YYYY/MM/DD"
);
const name = window.prompt("Please Enter your Name");
const age = calculateAge(displayCheck);
if (age < 18) {
window.alert(
"You Submitted that you are " + age + "years old, " + "" + name + "."
);
window.alert(
"You must be at least eighteen years old to visit this site. You will be re-directed to an
appropriate site shortly!"
);
window.location = "http://disney.com";
} else if (age > 18 && age < 101) {
window.alert(
"You are old enough to visit the site," + " " + "Welcome " + "" + name + "!"
);
document.write(name)
} else {
window.alert("You must be honest about your age, " + name + ".");
}
<h1 class="display-2">
Hey
<script src="script.js">
document.write(name)
</script>!
</h1>
好消息是它有效,但代码验证器不喜欢它。
还有什么其他方法可以做到这一点?
谢谢
【问题讨论】:
-
永远不要使用 document.write stackoverflow.com/questions/802854/….
标签: javascript html css verification