How can I limit possible inputs in a HTML5 “number” element?

前端 未结 26 2547
感动是毒
感动是毒 2020-11-22 05:12

For element, maxlength is not working. How can I restrict the maxlength for that number element?

26条回答
  •  遥遥无期
    2020-11-22 05:23

    This might help someone.

    With a little of javascript you can search for all datetime-local inputs, search if the year the user is trying to input, greater that 100 years in the future:

    $('input[type=datetime-local]').each(function( index ) {
    
        $(this).change(function() {
          var today = new Date();
          var date = new Date(this.value);
          var yearFuture = new Date();
          yearFuture.setFullYear(yearFuture.getFullYear()+100);
    
          if(date.getFullYear() > yearFuture.getFullYear()) {
    
            this.value = today.getFullYear() + this.value.slice(4);
          }
        })
      });
    

提交回复
热议问题