我有三个输入,分别是日,月和年。
<div id="birthday">
<div>
<label for="day">Day</label>
<input type="number" id="day" placeholder="Day" name="day" ref="day" />
</div>
<div>
<label for="month">Month</label>
<input type="number" id="month" placeholder="Month" name="month" ref="month" />
</div>
<div>
<label for="year">Year</label>
<input type="number" id="year" placeholder="Year" name="year" ref="year" />
</div>
<span class="clear_both"></span>
</div>
我想通过以下方式验证日期:
年
月
天
我只能查看月份和年份:
let day = this.refs.day.value
let month = this.refs.month.value
let year = this.refs.year.value
let errors = []
if (!((year.length == 4) && (year > 1900 && year < 2016))) {
errors.push("year");
}
if (!(month > 0 && month < 13)) {
errors.push("month");
}
如何在javascript中完成此工作?请你帮助我好吗。谢谢。
答案 0 :(得分:3)
这可能会对你有帮助。
function daysInMonth(m, y) { // Month is indexed as 0-11
switch (m) {
case 1 :
return (y % 4 === 0 && y % 100) || y % 400 === 0 ? 29 : 28;
case 8 : case 3 : case 5 : case 10 :
return 30;
default :
return 31
}
}
function isValid(d, m, y) {
return m >= 0 && m < 12 && d > 0 && d <= daysInMonth(m, y);
}
答案 1 :(得分:2)
在这里回答我自己的问题。安德里亚的回答向我指出了正确的方向。虽然有一个错误,我在这个答案中清除了。错误是,如果月份大于let day = this.refs.day.value
let month = this.refs.month.value
let year = this.refs.year.value
var monthLength = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
var total_days = 0;
// Adjust for leap years
if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
monthLength[1] = 29;
let errors = []
console.log('total_days = ' + total_days);
if (!((year.length == 4) && (year > 1900 && year < 2016))) {
errors.push("year");
}
if (!(month > 0 && month < 13)) {
errors.push("month");
total_days = monthLength[0];
}
else {
total_days = monthLength[month - 1];
}
if (!(day > 0 && day <= total_days)) {
errors.push("day");
}
中的项目列表,那么这将是错误的一天。还更改为检查所选月份的天数是否在1到总天之间。所以这是完整的脚本:
<table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>ID</th>
<th>Message Type</th>
<th>Crime Type</th>
<th>Description</th>
<th>Date/Time Reported</th>
<th>Date/Time Committed</th>
<th>Sender</th>
<th>Status</th>
<th>Options</th>
</tr>
</thead>
<tfoot>
<tr>
<th>--</th>
<th>--</th>
<th>--</th>
<th>--</th>
<th>--</th>
<th>--</th>
<th>--</th>
<th>--</th>
</tr>
</tfoot>
<tbody>
<?php
$query = "SELECT reports.id, types.`name`, classifications.`name` as class, reports.description,
concat(DATE_FORMAT(reports.committed_at,'%b %e, %Y'), ' at ', TIME_FORMAT(reports.committed_at,'%l:%i:%p')) as committed_at,
concat(DATE_FORMAT(reports.created_at,'%b %e, %Y'), ' at ', TIME_FORMAT(reports.created_at,'%l:%i:%p')) as created_at,
reports.casestat, users.`name` as sender, concat(status.id, ' - ', status.status) as status FROM reports
INNER JOIN types ON reports.type_id = types.id
INNER JOIN classifications ON reports.classification_id = classifications.id
INNER JOIN users ON reports.created_by = users.id
INNER JOIN status ON reports.casestat = status.id";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
switch($row['casestat']){
case 0: $rwcol="#ff3c3c"; break;
case 1: $rwcol="#ffb93c"; break;
case 2:$rwcol="#ffff3c"; break;
case 3: $rwcol="#00bc00"; break;
case 4: $rwcol="#5a5aff"; break;
}
echo "<tr bgcolor=$rwcol>";
echo "<td id='code'>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['class'] . "</td>";
echo "<td>" . $row['description'] . "</td>";
echo "<td>" . $row['created_at'] . "</td>";
echo "<td>" . $row['committed_at'] . "</td>";
echo "<td>" . $row['sender'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td><a onclick='updateStat(this.id)' id='".$row['id']."' href='#' ><abbr title='Edit'><i class='fa fa-edit'></i></abbr>Update Status</a> </tr>";}
?>
</tbody>
答案 2 :(得分:1)
类似于user3817980,并且基于此question但内置于您的代码中,请参阅下文。
我也在检查这一天是不是消极的,这可能是一种过度杀伤但不会造成伤害。
let day = this.refs.day.value
let month = this.refs.month.value
let year = this.refs.year.value
var monthLength = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
// Adjust for leap years
if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
monthLength[1] = 29;
let errors = []
if (!((year.length == 4) && (year > 1900 && year < 2016))) {
errors.push("year");
}
if (!(month > 0 && month < 13)) {
errors.push("month");
}
if (day < 0 || day > monthLength[month - 1]) {
errors.push("day");
}