Javascript方法确保日期有效 [英] Javascript method to ensure that a date is valid

查看:89
本文介绍了Javascript方法确保日期有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着编写一个方法来检查日期是否有效。它以三个字符串传递:月,日,年,依次排列。月应该从0-11而不是1-12。我已经测试了Java中的代码的逻辑,它的工作原理。 is_int是另一种测试字符串是否仅由数字字符组成的方法。

 函数is_int(value){
for ((value.charAt(i)<'0')||(value.charAt(i)>'9'))(i = 0; i< value.length; i ++)返回false
}
返回true;


函数isValidDate(value1:String,value2:String,value3:String)
{
if(!is_int(value3))return false;
if(!is_int(value2))返回false;
if(!is_int(value1))返回false;
var v1 = parseInt(value1)+ 1;
var v2 = parseInt(value2);
var v3 = parseInt(value3);
if(v1> 12 || v1< 1)返回false;
if(v2> 31 || v2< 1)返回false;
if(v2 == 31)if(v1 == 2 || v1 == 4 || v1 == 6 || v1 == 9 || v1 == 11)return false;
if(v1!= 2)返回true;
if(v2 <29)返回true;
if(v2 == 30)return false;
if(v3%400 == 0)
返回true;
else if(v3%100 == 0)
return false;
else if(v3%4 == 0)
return true;
else
返回false;
}

我的第一个测试人员要求输入三个文本,如果isValidDate函数返回false,显示警报。如果不是,则转发到空白的html页面。然而,我试过这个:

$ $ p $ function validate(){
if(!isValidDate(a,a ,a)){
alert(wrong);
返回false;
}
}

并且警报从不显示,并且每次都转发。奇怪的是,当我删除isValidDate前面的感叹号时,仍然发生这种情况。我也尝试交换单引号双引号,但也没有解决问题。我的测试仪发生is_int时也会发生同样的情况。我不知道我在哪里出错。

解决方案

我知道你没有要求,但是这是一个<

 函数isValidDate(())更多有效的方法来检查日期,并且它更一致地处理奇数日期:年,月,日){
var d = new Date(parseInt(year,10),parseInt(month,10)-1,parseInt(day,10),0,0,0);
return d.getFullYear()== year&&
(d.getMonth()+ 1)== month&&
d.getDate()== day;
}

isValidDate(2011,08,04); // true
isValidDate(bob,08,04); // false
isValidDate(1979,1,1); // true

小提琴: http://jsfiddle.net/2WJCv/



替代HTML链接: http://pastehtml.com/view/b2se2lk9k.html


I'm trying to write a method that checks whether a date is valid. It is passed in three Strings: month, day, and year, in order. Month should be from 0-11 instead of 1-12. I have tested the logic of the code in Java and it works. is_int is another method that tests if a String is composed solely of numerical characters. Unfortunately, I am running into problems which I can't figure out.

function is_int(value) {
   for (i = 0 ; i < value.length ; i++) {
      if ((value.charAt(i) < '0') || (value.charAt(i) > '9')) return false
    }
   return true;
}

function isValidDate(value1:String, value2:String, value3:String)
{
  if (!is_int(value3)) return false;
  if (!is_int(value2)) return false;
  if (!is_int(value1)) return false;
  var v1 = parseInt(value1) + 1;
  var v2 = parseInt(value2);
  var v3 = parseInt(value3);
  if (v1 > 12 || v1 < 1) return false;
  if (v2 > 31 || v2 < 1) return false;
  if (v2 == 31) if (v1 == 2 || v1 == 4 || v1 == 6 || v1 == 9 || v1 == 11) return false;
  if (v1 != 2) return true;
  if (v2 < 29) return true;
  if (v2 == 30) return false;
  if (v3 % 400 == 0)
        return true;
  else if (v3 % 100 == 0)
        return false;
  else if (v3 % 4 == 0)
        return true;
  else
        return false; 
}

My first tester is something that asks for three text inputs and, if the isValidDate function returns false, displays an alert. If not, it forwards to a blank html page. However, I tried this:

function validate() {
  if (!isValidDate("a", "a", "a")) {
  alert("wrong");
  return false;
  }
}

and the alert never displayed and it forwarded every time. Strangely enough, this still happened when I removed the exclamation point in front of isValidDate. I also tried swapping the double quotation marks for single, but that didn't fix the problem either. The same thing happens with my tester for is_int. I have no idea where I'm going wrong.

解决方案

I know you didn't ask for it, but here's a much more valid way to check for dates, and it handles odd dates more consistently:

function isValidDate(year, month, day) {
    var d = new Date(parseInt(year,10), parseInt(month, 10)-1, parseInt(day, 10), 0, 0, 0);
    return d.getFullYear() == year &&
           (d.getMonth()+1) == month &&
           d.getDate() == day;
}

isValidDate("2011", "08", "04"); // true
isValidDate("bob", "08", "04"); // false
isValidDate("1979", "1", "1"); // true

Fiddle with it: http://jsfiddle.net/2WJCv/

Alternate HTML-only link: http://pastehtml.com/view/b2se2lk9k.html

这篇关于Javascript方法确保日期有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆