html输入类型=日期,字段约束

时间:2017-08-06 05:57:15

标签: html html5 date

我有简单输入,如:

Enter Payment Date : <input type="date" name="date" required><br/><br/>

现在我想限制用户选择当前日期之前的日期。我检查了日期输入的属性,发现我可以设置最小和最大日期。但min属性要求我指定一个特定的日期,如min="2017-08-06"。但我希望它成为系统的当前日期。因为如果我将最小日期设置为2017-08-06并且用户稍后在2017-08-20使用该应用程序,那么用户可以选择2017-08-15左右的日期,我不会这样做想要。我希望用户能够从他/她使用该应用程序的当前日期中选择日期。

使用此输入字段有什么方法可以吗?

对不起,这是一个很长的解释,我想清除它。

1 个答案:

答案 0 :(得分:1)

您必须使用JavaScript获取当前日期,然后从中解析一些信息。该代码取自Samuel Meddows'已接受的问题How do I get the current date in JavaScript的回答,然后进行调整以使用HTML def gpacalculator(grade,credit): gradescore = 0 gradescore = int(gradescore) if grade in ("A","Z"): gradescore = 4*credit elif grade == "B+": gradescore = 3.5*credit elif grade == "B": gradescore = 3*credit elif grade == "C+": gradescore = 2.5*credit elif grade == "C": gradescore = 2*credit elif grade == "D+": gradescore = 1.5*credit elif grade == "D": gradescore = 1*credit elif grade == "F": gradescore = 0*credit return gradescore subject = input("How many subjects would you like to calculate?") subject = int(subject) print("The different levels of grades are: Z, A, B+, B, C+, C, D+, D, F") totalgradescore = 0 totalcredit = 0 for a in range(subject): grade = str(input("What's your grade for subject number " + str(a+1) + "?")) grade = grade.upper() credit = int(input("What's the number of credits for subject number " + str(a+1) + "?")) totalcredit += credit totalgradescore += gpacalculator(grade,credit) gpa = totalgradescore / totalcredit print("Your GPA is: ", gpa) 标记的min属性。

input
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();

if (dd < 10) {
  dd = '0' + dd
}

if (mm < 10) {
  mm = '0' + mm
}

today = yyyy + '-' + mm + '-' + dd;
document.getElementById('input').setAttribute("min", today);