Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何以mm:dd:yy格式验证javascript中的日期用户输入_Javascript_Html - Fatal编程技术网

如何以mm:dd:yy格式验证javascript中的日期用户输入

如何以mm:dd:yy格式验证javascript中的日期用户输入,javascript,html,Javascript,Html,我尝试使用以下代码验证日期:- function d(id){ var n= document.getElementById(id); var re=/^(?:(0[1-9]|1[012])[\- \/.](0[1-9]|[12][0-9]|3[01])[\- \/.](19|20)[0-9]{2})$/; if (re.test(n.value)) { n.style.backgroundColor="#52F40C"; } else { w

我尝试使用以下代码验证日期:-

    function d(id){
 var n= document.getElementById(id);
 var re=/^(?:(0[1-9]|1[012])[\- \/.](0[1-9]|[12][0-9]|3[01])[\- \/.](19|20)[0-9]{2})$/;
  if (re.test(n.value))
  {
     n.style.backgroundColor="#52F40C";

  }
  else
  {
    window.alert("Invalid date");
               n.style.backgroundColor="#F40C0C";
               n.focus();
               n.value="";

  }
}
但它不起作用。这段代码有什么问题?

试试这个

 function isValidDate(subject){
  if (subject.match(/^(?:(0[1-9]|1[012])[\- \/.](0[1-9]|[12][0-9]|3[01])[\- \/.](19|20)[0-9]{2})$/)){
    return true;
  }else{
    return false;
  }
}

尝试使用这个函数

function isDate(txtDate, separator) {
var aoDate,           // needed for creating array and object
    ms,               // date in milliseconds
    month, day, year; // (integer) month, day and year
// if separator is not defined then set '/'
if (separator === undefined) {
    separator = '/';
}
// split input date to month, day and year
aoDate = txtDate.split(separator);
// array length should be exactly 3 (no more no less)
if (aoDate.length !== 3) {
    return false;
}
// define month, day and year from array (expected format is m/d/yyyy)
// subtraction will cast variables to integer implicitly
month = aoDate[0] - 1; // because months in JS start from 0
day = aoDate[1] - 0;
year = aoDate[2] - 0;
// test year range
if (year < 1000 || year > 3000) {
    return false;
}
// convert input date to milliseconds
ms = (new Date(year, month, day)).getTime();
// initialize Date() object from milliseconds (reuse aoDate variable)
aoDate = new Date();
aoDate.setTime(ms);
// compare input date and parts from Date() object
// if difference exists then input date is not valid
if (aoDate.getFullYear() !== year ||
    aoDate.getMonth() !== month ||
    aoDate.getDate() !== day) {
    return false;
}
// date is OK, return true
return true;
函数isDate(txtDate,分隔符){
var aoDate,//创建数组和对象所需
ms,//日期(毫秒)
月、日、年;//(整数)月、日、年
//如果未定义分隔符,则设置“/”
如果(分隔符===未定义){
分隔符='/';
}
//将输入日期拆分为月、日和年
aoDate=txtDate.split(分隔符);
//数组长度应正好为3(不多不少)
如果(aoDate.length!==3){
返回false;
}
//从数组中定义月、日和年(预期格式为m/d/yyyy)
//减法将变量隐式转换为整数
month=aoDate[0]-1;//因为JS中的月份从0开始
day=aoDate[1]-0;
年份=aoDate[2]-0;
//测试年范围
如果(年份<1000 | |年份>3000){
返回false;
}
//将输入日期转换为毫秒
ms=(新日期(年、月、日)).getTime();
//从毫秒开始初始化Date()对象(重用aoDate变量)
aoDate=新日期();
aoDate.setTime(毫秒);
//比较输入日期和来自日期()对象的零件
//如果存在差异,则输入日期无效
如果(aoDate.getFullYear()!==年||
aoDate.getMonth()!==月份||
aoDate.getDate()!==天){
返回false;
}
//日期是确定的,返回true
返回true;

}

您遇到了什么错误?对于任何案例,都不会引发警报框。请扫描您发布的html,以查看此函数中出现了什么类型的
参数。日期:Dude您以前发布了一些内容,现在对其进行了编辑。你能重新发布你以前的答案吗?是的,即使是一个有效的条目,也只会执行失败案例