正则表达式可在键入时验证日期mm / dd / yyyy [英] Regex to validate date mm/dd/yyyy while typing

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

问题描述

我正在尝试为自定义文本字段编写一个正则表达式,以便在发生更改时将测试输入,如果测试失败,将不允许用户编写该字符。

I'm trying to write a regex for a custom text field so that, when a change occurs the input will be tested and if the test fails, user won't be allowed to write that character.

我知道也有类似的问题询问,但我无法适应该正则表达式。

I know there is a similar question asked, but I couldn't manage to adapt that regex.

到目前为止,我已经花了很多编写

So far I've spent a lot of time writing a regex like

^(([[9-9] {0} | [1-9] {1} \ /)|(1(([0-2] {0,1})|([0-2] { 0,1} \ /)))|((0)|(0 [1-9] {1})|(0 [1-9] {1})\ /))(([[1-9 ] {0,1} | [1-9] \ /)|((((1 | 2)([0-9] {1})))|(((1 | 2)([0-9] {1 }} \ /))|((3(0 | 1))|(3(0 | 1)\ /))|((0)|(0 [1-9] {1})|(0 [1-9] {1} \ /)))(([[1-9] {0,4})|([1-9] {1} [0-9] {0,3}))$

,但仍然允许一些不需要的输入,例如:

but still it allows some unwanted inputs such as:


  • 1212/2012

  • 12121212

  • 12/121212

任何帮助将不胜感激。

编辑:

每次用户键入 单个字符 时,Regex都应进行验证,因此这些输入也应通过regex:

Regex should validate each time user types a single character, so these inputs should pass regex too:


  • 12 /

  • 12/0

  • 12/01

  • 12/01 /

  • 12/01/2

  • 12/
  • 12/0
  • 12/01
  • 12/01/
  • 12/01/2

推荐答案

^([0-9] {1,2}){1}(\ / [0-9] {1,2}){0,1}(\ / [0-9] {0,4}){0,1} $

^([0-9]{1,2}){1}(\/[0-9]{1,2}){0,1}(\/[0-9]{0,4}){0,1}$

这篇关于正则表达式可在键入时验证日期mm / dd / yyyy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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