如何在javascript中以dd / MM / yyyy格式验证日期? [英] How to validate date in dd/MM/yyyy format in javascript?

查看:105
本文介绍了如何在javascript中以dd / MM / yyyy格式验证日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在javascript中以dd / MM / yyyy格式验证日期?



我使用下面的正则表达式。它适用于所有闰年计算。也适用于带有斜杠字符的数据。但验证不适用于输入数值输入。我还需要在1900年之前阻止日期。



我的正则表达式代码如下:



  var  dateformat = / ^(( 0  [1-9] | [ 12 ] [0-9] | 3 [ 01 ])(\ /)( 0  [ 13578 ] | 1 [ 02 ]))|(( 0  [1-9] | [ 12 ] [0-9])(\ /)( 02 ))|(( 0  [1-9] | [ 12 ] [0-9] | 3 [ 0 ]) (\ /)( 0  [ 469 ] | 11))(\ /)\ d { 4 } $ /; 

解决方案

/;


您可以使用 http://momentjs.com/ [ ^ ]。您需要做的就是,

时刻('  31/12/2012''  DD / MM / YYYY' true )。isValid();  //   true  



参考: http://momentjs.com/docs/#/parsing/string-format / [ ^ ]



问候..


您可以使用正则表达式来实现,但它没有任何实际意义。



让我向您解释一个与验证相关的非常一般的想法,它适用于许多平台,技术和语言。



所有,想想你为什么需要验证?如果您打算从该字符串中获取 Date 值,则只需验证字符串。因此,您不需要验证本身,您只需要知道字符串是否足够好用于创建 Date 对象: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference / Global_Objects / Date [ ^ ]。



但是当你从字符串中得到 Date 对象时,结果是否有效。如果您可以一步完成验证和构建,为什么要执行两个单独的步骤?与正则表达式尝试相比,这种通过构造验证不容易出错,因为构造函数知道更好什么是有效的,什么不是。



JavaScript Date 构造函数的具体特性是:它们不抛出异常,不返回错误代码,什么都没有。这是JavaScript技术的典型特征。因此,您只需要获取 Date 对象并验证它,而不是字符串。什么是验证方法?我建议这个:

  function  isValid(date){
var value = date.valueOf();
if (value === 0 返回 true ;
else return !! value;
}

// 用法示例
< span class =code-keyword> var a = new 日期 12/11/2013 11:20:13);
if (isValid(a)){ / * ... * / } // 有效!

var b = new 日期 ?? / 11/2013 11:20:13);
if (isValid(b)){ / * ... * / } // 无效!



我希望它很清楚。在有效时间值的情况下,函数 Date.prototype.valueOf()返回一个整数值,即毫秒数,这是从某一点开始的时间跨度。 1970年的时间:

https:// developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/valueOf [ ^ ]。



如果无效日期,我试过的浏览器返回NaN,但我没有用标准检查它。即使我假设不同的浏览器可以做其他事情,他们仍然应该为有效的 Date 对象返回一个数字。 '!!'将结果转换为布尔值,当时间有效时,它将在所有情况下都为真。零是一种特殊情况:它将被转换为False,但肯定是有效的。 '==='(不是'==')很重要:它与数字类型的0比较,而不是混合它,比如说0。



-SA


How to validate date in dd/MM/yyyy format in javascript?

I am using the below regular expression. It works for all leap year calculation. Also for data with slash characters. But validation is not working for entry of numeric value entry alone.Also I need to block a date before 1900.

My Regular expression code is as below:

var dateformat = /^((0[1-9]|[12][0-9]|3[01])(\/)(0[13578]|1[02]))|((0[1-9]|[12][0-9])(\/)(02))|((0[1-9]|[12][0-9]|3[0])(\/)(0[469]|11))(\/)\d{4}$/;

解决方案

/;


You can use http://momentjs.com/[^]. All you need to do is,

moment('31/12/2012', 'DD/MM/YYYY',true).isValid();//true


Refer : http://momentjs.com/docs/#/parsing/string-format/[^]

Regards..


You can do it with Regular Expressions, but it makes no practical sense.

Let me explain you a very general idea related to validation, which is applicable to many platforms, technologies and languages.

First of all, think why would you need validation? You only need to validate your string if you plan to get a Date value from that string. So, you don't need validation per se, you only need to know if the string is "good enough" to be uses for creation of the Date object: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date[^].

But when you get a Date object from string, the result can be valid or not. Why doing two separate steps if you can do validation and construction in one step? This "validation through construction" is not error-prone, in contrast to your Regular Expression attempts, because the constructor "knows better" what is valid and what not.

The specific feature of JavaScript Date constructors is: they don't throw exception, don't return "error code", nothing. It's very typical for JavaScript technology. So, you simply need to obtain Date object and validate it, not string. What would be the validation method? I would suggest this one:

function isValid(date) {
   var value = date.valueOf();
   if (value === 0) return true;
   else return !!value;
}

// usage example
var a = new Date("12/11/2013 11:20:13");
if (isValid(a)) { /* ... */ } // valid!

var b = new Date("??/11/2013 11:20:13");
if (isValid(b)) { /* ... */ } // invalid!


I hope it's clear. In case of the valid time value, the function Date.prototype.valueOf() returns an integer value, the number of milliseconds, which is the time span from the certain point of time in 1970:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/valueOf[^].

In case of invalid date, the browsers I tried return NaN, but I did not check it up with the standard. Even if I assume that different browsers can do something else, they still should return a number for a valid Date object. '!!' converts result to Boolean, which will be true in all cases when the time is valid. Zero is a special case: it would be converted to False, but is certainly valid. '===' (not '==') is important: it compares with 0 of numeric type, not mixing it, with, say, "0".

—SA


这篇关于如何在javascript中以dd / MM / yyyy格式验证日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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