gpt4 book ai didi

Javascript - 日期验证 IF 需要语句

转载 作者:行者123 更新时间:2023-12-03 10:37:39 26 4
gpt4 key购买 nike

所以我已经写了练习的这一部分,但我需要一个使用“Date”和“dateOfBirth.getFullYear() == Year”等对象的 IF 语句来验证用户是否输入了正确的日期。一个简单的解决方案将非常感激。如何放入 IF 语句来测试用户输入的生日是否正确,如果错误则拒绝?

//Date Validation
var canvas;
canvas = openGraphics();

var phrase1;
phrase1 = "Name: ";

var name;
name = prompt( "Please enter your name:" );

var phrase2;
phrase2 = "<br />Mobile Number: ";

var mobile;
mobile = prompt( "Please enter your mobile number:" );

var phrase3;
phrase3 = "<br />E-mail Address: ";

var email;
email = prompt( "Please enter your E-mail Address:" );

var date;
date = prompt( "What day were you born on? e.g. 21st");
date = parseInt(date, 10);

var phrase4 = "<br /> Date of Birth: ";

var month;
month = prompt( "What month were you born in? e.g. April");

var phrase5 = " ";

var year;
year = prompt( "What year were you born in? e.g. 1996");

var phrase6 = " ";

var age;
var a = 2014;
var b = year;

age = a - b;

var phrase7 = "<br />Approximate Age: ";

var message;

message = phrase1 + name + phrase2 + mobile + phrase3 + email + phrase4 + date + phrase5 + month + phrase6 + year + phrase7 + age;


canvas.drawString(message, 10, 10 );
canvas.setFont( "calibri", "16px", Font.ITALIC );
canvas.paint();

最佳答案

function checkDate(date) {
return new Date(date).getFullYear() == 2014
}

var date = null

while( ! checkDate(date)) {
date = prompt('Please enter a date')
}

alert('Success, your date is: ' + date)

http://jsfiddle.net/uvfxwkzo/1/ (例如,您应该输入 2014-02-02)

或者,您可以递归地执行:

http://jsfiddle.net/uvfxwkzo/2/

关于Javascript - 日期验证 IF 需要语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28924591/

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