在javascript中验证出生日期
我想通过JavaScript函数验证生日 这是JavaScript函数:在javascript中验证出生日期,javascript,php,Javascript,Php,我想通过JavaScript函数验证生日 这是JavaScript函数: function dat() { var myDate1 = document.getElementById("d").value; var month = myDate1.substring(0, 2) - 1; var date = myDate1.substring(3, 5) - 0; var year = myDate1.substring(6, 10) - 0;
function dat() {
var myDate1 = document.getElementById("d").value;
var month = myDate1.substring(0, 2) - 1;
var date = myDate1.substring(3, 5) - 0;
var year = myDate1.substring(6, 10) - 0;
var myDate = new Date(year, month, date);
var today = new Date();
if (myDate1 > today) {
document.getElementById('dd').innerHTML = "";
document.getElementById("d").style.borderColor = "green";
}
else if (myDate1 < today) {
document.getElementById('dd').innerHTML = myDate1;
document.getElementById("d").style.borderColor = "red";
}
if (document.getElementById("d").value == "") {
document.getElementById('dd').innerHTML = "This Field Is Required";
document.getElementById("d").style.borderColor = "red";
}
}
函数dat(){
var myDate1=document.getElementById(“d”).value;
var month=myDate1。子字符串(0,2)-1;
var date=myDate1。子字符串(3,5)-0;
var year=myDate1.子串(6,10)-0;
var myDate=新日期(年、月、日);
var today=新日期();
如果(myDate1>今天){
document.getElementById('dd').innerHTML=“”;
document.getElementById(“d”).style.borderColor=“绿色”;
}
else if(myDate1<今天){
document.getElementById('dd').innerHTML=myDate1;
document.getElementById(“d”).style.borderColor=“红色”;
}
if(document.getElementById(“d”).value==“”){
document.getElementById('dd').innerHTML=“此字段为必填字段”;
document.getElementById(“d”).style.borderColor=“红色”;
}
}
这是html输入
<div class="form-group">
<label for="d">Date Of Birth</label>
<div class="input-group">
<input type="date" class="form-control" name="d" id="d"
onKeyUp="dat()" placeholder="mm/dd/yyyy"/>
<p>
<div style="color:red" id="dd"></div>
</p>
<span class="input-group-addon"></span>
</div>
出生日期
它总是返回(日期是左),即使我把明天的日期
问题是什么?您将日期与输入值进行比较,而不是与日期对象进行比较
var myDate1= document.getElementById("d").value; <-- You are using this
var myDate= new Date(year,month,date); <-- Not this
var today = new Date();
if (myDate1>today)
^^^^^^^
var myDate1=document.getElementById(“d”)值 如果您使用的是mm/dd/yyyy
格式的日期,您可以简单地使用Date.parse
而不是编写自己的日期解析器
这样做:
var utime = Date.parse(yourDate)
if (utime == NaN) {
alert('Invalid date');
} else if (utime < new Date()) {
alert('Valid birth date! It\'s in the past');
} else {
alert('Invalid birth date! It\'s in the future');
}
在Firefox 30和Firefox中将被视为2015年11月25日的本地日期
Safari 7中的日期无效。但是,如果字符串被识别为
如果ISO格式字符串包含无效值,它将返回
符合ES5的所有浏览器中的NaN:
// ISO string with invalid values
new Date('2014-25-23').toISOString();
// returns "RangeError: invalid date" in all es5 compliant browsers
SpiderMonkey的特定于实现的启发式可以在
jsdate.cpp。字符串“10 06 2014”是不合格的一个示例
ISO格式,因此返回到自定义例程。也见此
关于解析工作原理的粗略概述
new Date('10 06 2014');
将被视为2014年10月6日而非6月10日的当地日期,
2014其他例子:
new Date('foo-bar 2014').toString();
// returns: "Invalid Date"
Date.parse('foo-bar 2014');
// returns: NaN
你真的应该更好地格式化你的代码。它很难按原样阅读。myDate1>今天
在未来的myDate1
中总是正确的。你确定你没有简单地混淆比较顺序,意思是myDate
?无论是date
还是year
都将是NaN
与该代码一起使用,即使我将myDate2016-12-22
,这意味着OP在一个月内得到20
,可能类似于6-
的日期等等?如果你想依赖未记录的行为,是的,并且担心它是用UTC还是当地时间解释的(这可能会影响你最终看到的日期)。例如,Date.parse(“01/06/2015”)
在Firefox上显示2015年1月6日,而在某些地方,Date.parse(“06/01/2015”)
在Firefox上显示6月5日。(因为DST,所以UTC和语言环境时间是偏移的。)Chrome将UTC用于相同的字符串。是的,你是对的。这根本不是一种安全的方法,最好的选择是使用日期/时间库,例如,但它确实执行操作代码所执行的操作。
new Date('foo-bar 2014').toString();
// returns: "Invalid Date"
Date.parse('foo-bar 2014');
// returns: NaN