正则表达式年龄验证 [英] Regex age verification

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

问题描述

任何人有年龄验证一个良好的正则表达式?



我有我在哪里验证什么用户输入是一个日期日期字段。



我基本上想弄清楚,如果该日期是有效的,然后进一步细化日期是在几年x个



也许这可能是太复杂,钉到不管我有。太远了,但我想这不会是

  ^([1] [012] | [0] [ 1-9])[/  - ]([3] [01] | [12] \d | [0] [1-9])[/  - ](\d {4} |?\d { 2})$ 


解决方案

我是一个大风扇,正则表达式,当然是可能的检查日期反对合法范围使用正则表达式



但是:



匹配有效日期(不包括像闰年检查花哨的东西)是相当微不足道,但讨厌用正则表达式 - 构建正则表达式像的 RegexMagic 将为您迅速做到这一点:

 (?:0 2 [/.-](?:[12][0-9]|0?[1-9])|(?:0?[469]|11)[/.-](?:30|[12] [0-9] | 0 [1-9])|(?:?0 [13578] | 1 [02])[/.-](?: 3 [01] | [12] [0-9] ?| 0 [1-9]))[/.-] [0-9] {4} 

是RegexMagic而不强加在日期范围限制生成MM / DD / YYYY日期验证(即2020年2月31日将不匹配,会01/31/4500)。



这已经是丑陋的,可能非常难搞清楚,如果你是我的唯一继承这个代码。



第二个问题,即对验证预定日期范围甚至丑陋。这是可以做到的,但




  1. 你需要改变你的正则表达式
    每一天 - 毕竟,每
    一天,有人满18岁,不是吗?
    以及


  2. 正则表达式会得到更加笨重。要允许从1992年6月10日任何东西,直到
    06/10/2010,你得到




这。正则表达式的怪物

  0 6 /.-](?: 30 |?[12] [0-9] )[/.-] 1992年|(?::( 0 9?|?11)[/.-](?: 30 | [12] [0-9] | 0 [1-9])|?
(:?0 [78] | 1 [02])[/.-](??: 3 [01] | [12] [0-9] | 0 [1-9]))[/。 - ] 1992年| 0 6 /.-](?: 10 | 0 [1-9]?)
[/.-] 2010 |(?:??0 2 [/.-](?: [12] [0-9] | 0 [1-9])|≠0 4 [/.-](?: 30 |?[12] [0-9] | 0 [1-9])|≠0 ?
[135] [/.-](?: 3 [01] | [12] [0-9] | 0 [1-9]))[/.-] 2010 |(?:0 λ2 [/.-](:[12] [0-9] | 0 [1-9]?)|
:| [/.-]((0 [469] 11?)? :30 | [12] [0-9] | 0 [1-9])|(?:??0 [13578] | 1 [02])[/.-](?: 3 [01] |
[12] [0-9] | 0 [1-9]))[/.-](?: 200 [0-9] | 199 [3-9])

(包括为清晰度换行)。



明天,你会需要

  0 6 /.-](?: 30 |?2 [0-9] | 1 [1-9 ])[/.-] 1992 |(?::( 0 9?|?11)[/.-](?: 30 | [12] [0-9] | 0 [1-9])| $? b $ b(0 [78] | 1 [02]?)[/.-](?: 3 [01] | [12] [0-9] | 0 [1-9]))[/ .-] 1992 | 0 6 /.-](?: 1 [01] | 0 [1-9])?
[/.-]2010|(?:0?2[/.- ](:?[12] [0-9] | 0 [1-9])|?0 4 [/.-](?: 30 | [12] [0-9] | 0 [1-9 ])| 0?
[135] [/.-](?: 3 [01] | [12] [0-9] | 0 [1-9]))[/.-] 2010 |(0? 2 [/.-](:[12] [0-9] | 0 [1-9]?)|
(:?0 [469] | 11)[/.-](?: 30 | [12] [0-9] | 0 [1-9])|(?:??0 [13578] | 1 [02])[/.-](?: 3 [01] |
〔12] [0-9] | 0 [1-9]))[/.-](?: 200 [0-9] | 199 [3-9])

注意的细微差别?



在短,因为所有其他人指出:这是不是你要使用正则表达式的东西。即使你。


Anyone have a good regex for Age verification?

I have a date field where I am validating that what the user enters is a date.

basically I wanted to figure out if that date is valid, and then further refine the date to be within x number of years

Perhaps this may be too complicated to tack onto whatever I have too far, but I figured it wouldn't be.

^([1][012]|[0]?[1-9])[/-]([3][01]|[12]\d|[0]?[1-9])[/-](\d{4}|\d{2})$

解决方案

I'm a big fan of regexes, and of course it's possible to check a date against a "legal range" using regexes.

BUT:

Matching a valid date (without fancy stuff like checking for leap years) is fairly trivial but tiresome by regex - a tool for constructing regexes like RegexMagic will do this for you quickly:

(?:0?2[/.-](?:[12][0-9]|0?[1-9])|(?:0?[469]|11)[/.-](?:30|[12][0-9]|0?[1-9])|(?:0?[13578]|1[02])[/.-](?:3[01]|[12][0-9]|0?[1-9]))[/.-][0-9]{4}

is what RegexMagic generates for MM/DD/YYYY date validation without imposing limits on date ranges (i.e. 02/31/2020 won't match, 01/31/4500 will).

This is already ugly and probably quite hard to figure out if you're the one inheriting this code.

The second problem, namely validating against a predefined date range is even uglier. It can be done, but

  1. you'd need to change your regex every single day - after all, every day, someone turns 18, don't they? and

  2. the regex would get even more unwieldy. To allow anything from 06/10/1992 until 06/10/2010, you get

this monster of a regex.

0?6[/.-](?:30|[12][0-9])[/.-]1992|(?:(?:0?9|11)[/.-](?:30|[12][0-9]|0?[1-9])|
(?:0?[78]|1[02])[/.-](?:3[01]|[12][0-9]|0?[1-9]))[/.-]1992|0?6[/.-](?:10|0?[1-9])
[/.-]2010|(?:0?2[/.-](?:[12][0-9]|0?[1-9])|0?4[/.-](?:30|[12][0-9]|0?[1-9])|0?
[135][/.-](?:3[01]|[12][0-9]|0?[1-9]))[/.-]2010|(?:0?2[/.-](?:[12][0-9]|0?[1-9])|
(?:0?[469]|11)[/.-](?:30|[12][0-9]|0?[1-9])|(?:0?[13578]|1[02])[/.-](?:3[01]|
[12][0-9]|0?[1-9]))[/.-](?:200[0-9]|199[3-9])

(linebreaks included for "clarity").

Tomorrow, you'll need

0?6[/.-](?:30|2[0-9]|1[1-9])[/.-]1992|(?:(?:0?9|11)[/.-](?:30|[12][0-9]|0?[1-9])|
(?:0?[78]|1[02])[/.-](?:3[01]|[12][0-9]|0?[1-9]))[/.-]1992|0?6[/.-](?:1[01]|0?[1-9])
[/.-]2010|(?:0?2[/.-](?:[12][0-9]|0?[1-9])|0?4[/.-](?:30|[12][0-9]|0?[1-9])|0?
[135][/.-](?:3[01]|[12][0-9]|0?[1-9]))[/.-]2010|(?:0?2[/.-](?:[12][0-9]|0?[1-9])|
(?:0?[469]|11)[/.-](?:30|[12][0-9]|0?[1-9])|(?:0?[13578]|1[02])[/.-](?:3[01]|
[12][0-9]|0?[1-9]))[/.-](?:200[0-9]|199[3-9])

Notice the subtle difference?

In short, as all the others have pointed out: This is not something you want to use a regular expression for. Even if you can.

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

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