在javascript中进行日期验证的正则表达式 [英] Regex for Date Validation in javascript

查看:91
本文介绍了在javascript中进行日期验证的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请有人给出日期验证正则表达式,这将允许以下规则

pls can somebody give the date validation regex, which will allow the following rules are


  1. 它应该允许 mm / dd / yyyy,m / d / yyyy,mm / d / yyyy,m / d / yyyy (不允许yy)

  2. 月份的天数(30和31)验证。

  3. 飞跃& 2月份验证非闰年。

  1. It should allow mm/dd/yyyy, m/d/yyyy, mm/d/yyyy, m/d/yyyy (not allow yy)
  2. Number of days for month (30 and 31) validation.
  3. Feb month validation for leap & non leap years.


推荐答案

试试这个:

([0-9][1-2])/([0-2][0-9]|[3][0-1])/((19|20)[0-9]{2}) 

然后如果你有效来自上述正则表达式的字符串然后使用字符串操作,执行以下操作:

and then if you got a valid string from the above regex then with string manipulations, do something like below:

if(/([0-9][1-2])\/([0-2][0-9]|[3][0-1])\/((19|20)[0-9]{2})/.test(text)){
    var tokens = text.split('/');  //  text.split('\/');
    var day    = parseInt(tokens[0], 10);
    var month  = parseInt(tokens[1], 10);
    var year   = parseInt(tokens[2], 10);
}
else{
    //show error
    //Invalid date format
}

这篇关于在javascript中进行日期验证的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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