【问题标题】:How can i make an input date limit when you are typing or chosing the date?当您输入或选择日期时,如何设置输入日期限制?
【发布时间】:2014-08-01 21:34:33
【问题描述】:

我想限制特殊年份: 例如:年

<input type="date" max="2200-01-01" min="1900-01-01" class="form-control" id="regdate"
                   placeholder="Registration Date">

对不起,我的表达不好... P.S WITHOUT SUBMIT !-当您输入时,您可以输入超过 2200 或低于 1900。

【问题讨论】:

  • 意思是您希望年份介于这两者之间,或者您只希望这两年?
  • 我认为this 会帮助您解决问题。
  • 介于 1900 和 2200 之间,但不是在我提交时...
  • this,可能还有this

标签: javascript jquery html


【解决方案1】:

你不能通过html来做,但是你可以用js来做,例如http://fiddle.jshell.net/5Sup8/

$(function () {
    $('input').change(function () {
        var val = $(this).val().split('-');
        console.log(val);
        if (val[0] < 1900 || val[0] > 2200) {
            $(this).addClass('error');
        } else {
            $(this).removeClass('error');
        }
    });
});

【讨论】:

  • 请不要提供仅链接的答案;在您的响应中直接包含一些代码。
  • 谢谢 :) 这是我正在寻找的答案:)
【解决方案2】:

您可以使用验证来实现这一点。一个简单的日期范围验证

function dateCheck() {
var fDate = new Date("1900/01/01");
var lDate; = new Date("2200/01/01");
fDate = Date.parse(document.getElementById("fDate").value);
lDate = Date.parse(document.getElementById("lDate").value);

if(fDate <= lDate) {
    alert("true");
    return true;
}
alert("false");
return false;

}

【讨论】:

    【解决方案3】:

    您的代码是正确的,将其包装在表单标签中,需要输入标签,它将起作用,当您尝试提交高于或低于最大值或最小值时,它将显示警报

                     <form>
              <input type="date" max="2200-01-01" min="1900-01-01" class="form-control" id="regdate"  placeholder="Registration Date" required />
                   <input type="submit" />
                     </form>
    

    Check the working fiddle here

    【讨论】:

      【解决方案4】:

      你可以参考下面的小提琴

      <body>
      
      <form>
      
        Enter a date before 1980-01-01 and after1900-01-01:
        <input type="date" name="bday" min="1901-12-31" max="1979-12-31"><br>
      
      
        <input type="submit">
      </form>
      </body>
      

      Demo

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-09
        • 1970-01-01
        • 2020-09-18
        • 1970-01-01
        相关资源
        最近更新 更多