简体   繁体   English

Javascript 中 MM/DD/YYYY 的正则表达式

[英]Regular Expression for MM/DD/YYYY in Javascript

I've just written this regular expression in javaScript however it doesn't seem to work, here's my function:我刚刚在 javaScript 中编写了这个正则表达式,但它似乎不起作用,这是我的函数:

function isGoodDate(dt){
    var reGoodDate = new RegExp("/^((0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2})*$/");
    return reGoodDate.test(dt);
}

and this is how I call it in my form validation这就是我在表单验证中调用它的方式

if(!isGoodDate(userInput[1].value)){
           alert("date not in correct format of MM/dd/YYYY");
           return false;  
        }

now I want it to return MM/DD/YYYY however if I put a valid date in it raises the alert?现在我希望它返回 MM/DD/YYYY 但是如果我在其中输入有效日期会引发警报? Any ideas anyone?任何人的想法?

Attention, before you copy+paste: The question contains some syntactic errors in its regex.注意,在复制+粘贴之前:该问题的正则表达式中包含一些语法错误。 This answer is correcting the syntax.这个答案正在纠正语法。 It is not claiming to be the best regex for date/time parsing.它并不声称是日期/时间解析的最佳正则表达式。

Try this:尝试这个:

function isGoodDate(dt){
    var reGoodDate = /^((0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2})*$/;
    return reGoodDate.test(dt);
}

You either declare a regular expression with:您可以声明一个正则表达式:

new RegExp("^((0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2})*$")

Or:或者:

/^((0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2})*$/

Notice the /注意/

Maybe because you are declaring the isGoodDate() function, and then you are calling the isCorrectDate() function? 也许是因为您声明了 isGoodDate()函数,然后调用了 isCorrectDate()函数?

Try:尝试:

function isGoodDate(dt){
    var reGoodDate = /^(?:(0[1-9]|1[012])[\/.](0[1-9]|[12][0-9]|3[01])[\/.](19|20)[0-9]{2})$/;
    return reGoodDate.test(dt);
}

Works like a charm, test it here .就像一个魅力,在这里测试一下

Notice, this regex will validate dates from 01/01/1900 through 31/12/2099 .请注意,此正则表达式将验证从01/01/190031/12/2099日期。 If you want to change the year boundaries, change these numbers (19|20) on the last regex block.如果要更改年份边界,请在最后一个正则表达式块上更改这些数字(19|20) Eg If you want the year ranges to be from 01/01/1800 through 31/12/2099 , just change it to (18|20) .例如,如果您希望年份范围从01/01/180031/12/2099 ,只需将其更改为(18|20)

Add this in your code, it working perfectly fine it here.将此添加到您的代码中,它在这里工作得很好。 click here http://jsfiddle.net/Shef/5Sfq6/点击这里http://jsfiddle.net/Shef/5Sfq6/

function isGoodDate(dt){
var reGoodDate = /^(?:(0[1-9]|1[012])[\/.](0[1-9]|[12][0-9]|3[01])[\/.](19|20)[0-9]{2})$/;
return reGoodDate.test(dt);

} }

I don't think you need a regular expression for this.我认为您不需要为此使用正则表达式。 Try this:尝试这个:

function isGoodDate(dt){
    var dts  = dt.split('/').reverse()
       ,dateTest = new Date(dts.join('/'));
    return isNaN(dateTest) ? false : true;
}

//explained
    var dts  = dt.split('/').reverse()
//      ^ split input and reverse the result
//        ('01/11/2010' becomes [2010,11,01]
//        this way you can make a 'universal' 
//        datestring out of it
       ,dateTest = new Date(dts.join('/'));
//     ^ try converting to a date from the 
//       array just produced, joined by '/'
    return isNaN(dateTest) ? false : true;
//         ^ if the date is invalid, it returns NaN
//           so, if that's the case, return false

I agree with @KooiInc, but it is not enough to test for NaN我同意@KooiInc,但它不足以测试 NaN

function isGoodDate(dt){
    var dts  = dt.split('/').reverse()
       ,dateTest = new Date(dts.join('/'));
    return !isNaN(dateTest) && 
       dateTest.getFullYear()===parseInt(dts[0],10) &&
       dateTest.getMonth()===(parseInt(dts[1],10)-1) &&
       dateTest.getDate()===parseInt(dts[2],10) 
}

which will handle 29/2/2001 and 31/4/2011这将处理 29/2/2001 和 31/4/2011


For this script to handle US dates do对于这个脚本来处理美国日期做

function isGoodDate(dt){
    var dts  = dt.split('/')
       ,dateTest = new Date(dt);
    return !isNaN(dateTest) && 
       dateTest.getFullYear()===parseInt(dts[2],10) &&
       dateTest.getMonth()===(parseInt(dts[0],10)-1) &&
       dateTest.getDate()===parseInt(dts[1],10) 
}

Validate (DD-MM-YYYY) format :)验证 (DD-MM-YYYY) 格式 :)

function isGoodDate(dt) {
    var reGoodDate = /^(?:(0[1-9]|[12][0-9]|3[01])[\-.](0[1-9]|1[012])[\-.](19|20)[0-9]{2})$/;
    return reGoodDate.test(dt);
}

Try the below code which accepts following date formats:尝试以下接受以下日期格式的代码:

MM-DD-YYYY, MM-DD-YY, DD-MM-YYYY, DD-MM-YY, MM/DD/YYYY, MM/DD/YY, DD/MM/YYYY, DD/MM/YY, MM\\DD\\YYYY, MM\\DD\\YY, DD\\MM\\YYYY, DD\\MM\\YY MM-DD-YYYY、MM-DD-YY、DD-MM-YYYY、DD-MM-YY、MM/DD/YYYY、MM/DD/YY、DD/MM/YYYY、DD/MM/YY、MM\\ DD\\YYYY、MM\\DD\\YY、DD\\MM\\YYYY、DD\\MM\\YY

function isGoodDate(dt) {
    var reGoodDate = /(?:((0\d|[12]\d|3[01])|(0\d|1[012]))[\-|\\|\/]((0\d|1[012])|(0\d|[12]\d|3[01]))[\-|\\|\/](((19|20)\d{2})|\d\d))/;
    return reGoodDate.test(dt);
}

(/^(0[1-9]|1[012])- /.- /.\\d\\d$/) 你可以使用这个肯定会工作,这适用于 MM/DD/YYYY

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM