【问题标题】:JavaScript Date of birth under 18JavaScript 18 岁以下的出生日期
【发布时间】:2018-06-27 08:22:43
【问题描述】:

有没有办法对完整的出生日期 (dd/mm/yyyy) 进行验证,以确定用户是否未满 18 岁。我发现的大多数教程只向我展示了如果用户年龄(不是 DOB)低于 18 岁的情况。 顺便说一句,我的 DOB 文本框也使用了“日期”输入类型。 HTML

<body>
    <header class="v-header container">
        <div class="fullscreen-backgorund-Booking">
        </div>
        <div>
            <ul>
                <li><a href="Home">Home</a></li>
                <li><a href="McLaren Vale">McLaren Vale</a></li>
                <li><a href="Barossa">Barossa</a></li>
                <li><a href="Clare">Clare</a></li>
                <li><a href="Online Booking">Online Booking</a></li>
                <div id="mySidenav" class="sidenav">
                    <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
                    <a href="Home">Home</a>
                    <a href="McLaren Vale">McLaren</a>
                    <a href="Barossa">Barossa</a>
                    <a href="Clare">Clare</a>
                    <a href="Online Booking">Online Booking</a>
                </div>
                <span id="sidebtn" onclick="openNav()">&#9776;</span>
            </ul>
        </div>

        <div class="header-overlay"></div>
        <div class="header-content" style="width: 100%;">
            <!-- width... is not working in external css... why?-->

            <h1 style="margin-top: -100px;">Online Booking</h1>
            <form name="myForm" action="/action_page.php" onsubmit="return validateForm()" method="post">
                <input type="text" name="fname" placeholder="Firstname"> Date of Brith
                <input type="date" name="DoB" placeholder="Date of Birth">
                <input type="text" name="TelNumber" placeholder="Telephone number">
                <input type="email" name="email" placeholder="Email"> Date of Tour
                <input type="date" name="DoT" placeholder="Date of tour (dd.mm.yyyy)">
                <form action="" id="calculate" onsubmit="return false;">

                    <input type="text" placeholder="Number of Travellers" id="val" name="val" value="" />
                    <label class="radiolabel">
                        <input type="radio" name="button" value="100">McLaren</label>
                    <label class="radiolabel">
                        <input type="radio" name="button" value="150">Barossa</label>
                    <label class="radiolabel">
                        <input type="radio" name="button" value="90">Clare</label>
                    <div id="result" style="display: block;">
                        Result:</div>
                    <input type="submit" id="submit" value="Submit" onclick="endresult(document.forms['myForm'].elements['button']);">
        </div>

        </form>
        </div>

【问题讨论】:

标签: javascript html validation date


【解决方案1】:

many similar questions,但似乎没有一个很好的答案。

与其计算年龄(这实际上比许多人预期的要复杂),您可以将出生日期加上 18 岁,看看它是否在今天之前,或者从今天减去 18 岁,看看它是否在出生日期之后.

// Subtract 18 years from now and see if greater than birth date
function over18(birthDate) {
  var now = new Date();
  var m =  now.getMonth();
  now.setFullYear(now.getFullYear() - 18);
  // deal with today being 29 Feb
  if (m != now.getMonth()) now.setDate(0);
  return now > birthDate;
}

// Some tests
var opts = {day: '2-digit', month:'short', year:'numeric'};
[new Date(2000,1,29), // 29 Feb 2000
 new Date(2000,0,20), // 20 Jan 2000
 new Date(2000,0,19), // 19 Jan 2000
 new Date(2000,0,18), // 18 Jan 2000
 new Date(1999,0,1)   //  1 Jan 1999
].forEach(function(d) {
  console.log(d.toLocaleString('en-GB', opts), over18(d));
});

一个简单的改进是允许一个可选的 checkDate 默认为今天并使用它而不是 now

【讨论】:

    【解决方案2】:

    我会先计算年龄,然后检查他是否超过18。这是一个coden-p。

    var birthday1 = new Date("2015-03-25");
    var birthday2 = new Date("1994-03-25");
    
    function isOverEighteen(birthday) {
        var ageDifMs = Date.now() - birthday.getTime();
        var ageDate = new Date(ageDifMs);
        var age = Math.abs(ageDate.getUTCFullYear() - 1970);
        if(age > 17){
          return true;
        }else{
          return false;
        }
    }
    
    console.log(isOverEighteen(birthday1));
    console.log(isOverEighteen(birthday2));

    【讨论】:

    • 可以帮助我并告诉我如何将上面的代码应用到我的 html 中。
    • new Date("2015-03-25") 将主要被视为 UTC(但不是所有浏览器),其中 Date.now 是本地的。
    猜你喜欢
    • 2018-11-17
    • 2016-09-25
    • 1970-01-01
    • 2022-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多