【问题标题】:Datepicker validation 18 years of age [closed]Datepicker 验证 18 岁 [关闭]
【发布时间】:2014-12-24 06:07:09
【问题描述】:

我正在使用来自 twitter bootstrap 的日期选择器,我想添加一些验证,即一旦您的日期低于 1​​8 岁,您的注册将不会被注册,我应该怎么做?这是我的生日示例 html 代码:

<label>Date of Birth*</label>
<input type="date" name="birth_date" tabindex="7" style="height: 30px;" required>

【问题讨论】:

  • 根据今天的日期 - 18 年 (html5) 设置 min 属性。但是,无论如何,您必须在服务器端和/或使用 js 进行检查。
  • 最好的方法是为了用户体验而进行javascript限制和为了安全而进行php限制。
  • 很抱歉,我不知道如何在 javascript 中执行此操作,我只是开发的初学者.. 很抱歉,有人会告诉我怎么做吗?
  • 您要么需要 (a) 复习基本的 Javascript 技能并逐步完成诸如此类的更复杂的事情,要么 (b) 聘请专业人士为您做这件事。

标签: javascript php html css twitter-bootstrap


【解决方案1】:

您需要使用 javascript 或您要使用的脚本语言来验证用户输入。

PHP 中的一个例子是:

<?php

if(isset($_POST['birth_date']))
{
   if($_POST['birth_date'] < 1996)
   {
      echo 'You can not register';
   }

}

【讨论】:

    【解决方案2】:

    如果您稍微向下看一下演示页面,您会看到“限制日期选择器”部分。使用下拉菜单指定“Year dropdown shows last 20 years”演示,点击查看源代码:

    $("#restricting").datepicker({ 
        yearRange: "-20:+0", // this is the option you're looking for
        showOn: "both", 
        buttonImage: "templates/images/calendar.gif", 
        buttonImageOnly: true 
    });
    

    你会想要做同样的事情(显然将 -20 更改为 -100 或其他东西)。

    或者你可以使用

    $('#dp1').datepicker({
        format: 'mm-dd-yyyy',
        startDate: '-15d',
        endDate: '+0d' // there's no convenient "right now" notation yet
    });
    

    【讨论】:

      猜你喜欢
      • 2021-12-22
      • 2022-01-22
      • 2010-12-21
      • 1970-01-01
      • 2022-07-01
      • 1970-01-01
      • 2018-05-11
      • 2023-04-04
      • 2019-03-14
      相关资源
      最近更新 更多