简体   繁体   English

日期,月份和年份下拉框

[英]Date, Month and year Dropdown boxes

I am creating a registration where I need to get the users Date Of Birth, my question is, How do I create dropdown boxes of month,year and day in html, and then later will process those days into a php script that will insert it to a database? 我正在创建一个注册,在此我需要获取用户的出生日期,我的问题是,如何在html中创建月份,年份和日期的下拉框,然后稍后将这些日子处理到将其插入的php脚本中到数据库? Where Month,year and day should have valid days for a certain month? 月份,年份和日期应该在某个月份具有有效日期?

EDIT: Live demo 编辑: 现场演示

How about this: 这个怎么样:

<select name="year"></select>
<select name="month">
    <option value="1">January</option>
    ...
</select>
<select name="day"></select>

And the JS part: 和JS部分:

var ysel = document.getElementsByName("year")[0],
    msel = document.getElementsByName("month")[0],
    dsel = document.getElementsByName("day")[0];
for (var i = 2000; i >= 1950; i--) {
    var opt = new Option();
    opt.value = opt.text = i;
    ysel.add(opt);
}
ysel.addEventListener("change", validate_date);
msel.addEventListener("change", validate_date);

function validate_date() {
    var y = +ysel.value, m = msel.value, d = dsel.value;
    if (m === "2")
        var mlength = 28 + (!(y & 3) && ((y % 100)!==0 || !(y & 15)));
    else var mlength = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][m - 1];
    dsel.length = 0;
    for (var i = 1; i <= mlength; i++) {
        var opt = new Option();
        opt.value = opt.text = i;
        if (i == d) opt.selected = true;
        dsel.add(opt);
    }
}
validate_date();

I'll let you work on the PHP part. 我将让您研究PHP部分。

A few caveats: 一些警告:

  1. I used addEventListener . 我使用了addEventListener Remember that IE<9 uses attachEvent instead. 请记住,IE <9改为使用attachEvent You can use the onchange property (deprecated) or rely on some JS framework like jQuery to do the job: $(msel).change(validate_date); 您可以使用onchange属性(不建议使用)或依靠某些JS框架(如jQuery)来完成此工作: $(msel).change(validate_date); .
  2. In older versions of Firefox, the add method of <select> elements is stupid and needs a second null argument. 在旧版本的Firefox中, <select>元素的add方法很愚蠢,需要第二个null参数。
  3. When the script computes mlength for February, it adds a boolean (a simple check for a leap year), which is casted to 0 or 1, to 28. This raises and exception in ECMAScript 5 strict mode. 当脚本计算2月的mlength ,它将添加一个布尔值(对check年的简单检查),将其强制转换为0或1到28。这会引发ECMAScript 5严格模式下的异常。

Just give them a plain text input. 只需给他们输入纯文本即可。 If you don't trust people to enter their birthday correctlly by typing, why will using select elements make any difference? 如果您不信任别人通过键入正确输入自己的生日,那为什么使用选择元素会有所不同呢? Just tell them the format you expect. 只需告诉他们您期望的格式即可。

eg 例如

<script>
function validateDate(v) {
  var bits = v.split('/');
  var d = new Date(bits[2], --bits[1], bits[0]);
  return d.getFullYear() == bits[2] && d.getMonth() == bits[1];
}

function validateForm(form) {
  if (!validateDate(form.birthday.value)) {
    alert('Birthday date is not a valid date');
    return false;
  }
}
</script>

<form onsubmit="return validateForm(this);" action="">
  <div>Birthday (day/month/year): <input type="text" name="birthday"> 
    <input type="submit">
  </div>
</form>

You can include a script verison that replaces the plain input with selects, but to cover a reasonable period you'll have about 100 entries in the year select, then you'll need to wait until they select both year and month before you can put in the days. 您可以包括一个脚本版本,该脚本版本可以用select替换普通输入,但是要覆盖一个合理的期间,select年内将有大约100个条目,那么您需要等到它们同时选择年份和月份后才能输入在过去。 At least the above gives you a simple algorithm for validating a date. 至少以上所述为您提供了一种用于验证日期的简单算法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM