Javascript RegEx:验证日期时间 [英] Javascript RegEx: validate date time

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

问题描述

我对正则表达式不太满意.正则表达式让我很沮丧.

I am not very good with regex. I am being frustrated by a regular expression.

示例:

我具有以下日期时间:2013年5月16日上午12:00

I have the following date time: 05/16/2013 12:00 am

我需要一个遵循以下格式的javascript正则表达式:mm/dd/yyyy hh:mm [am/pm]

I need a javascript regex that respect this format: mm/dd/yyyy hh:mm [am/pm]

 var dateReg = /^[0,1]?\d{1}\/(([0-2]?\d{1})|([3][0,1]{1}))\/(([1]{1}[9]{1}[9]{1}\d{1})|([2-9]{1}\d{3}))$/;
if(!dateReg.test(inputVal)) {
    $(this).after('<span class="error error-keyup-5">Invalid date format.</span>');
}

但是此代码仅适用于日期,不适用于时间.感谢您的帮助.

But this code works only for date, not with time. Thanks for your help.

推荐答案

您的代码段仅与日期匹配,没有原子与时间匹配.您的正则表达式应为:

Your code snippet does only match the date, there are no atoms to match the time. Your regexp should be:

dateReg = /^[0,1]?\d\/(([0-2]?\d)|([3][01]))\/((199\d)|([2-9]\d{3}))\s[0-2]?[0-9]:[0-5][0-9] (am|pm)?$/

这篇关于Javascript RegEx:验证日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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