Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
16
rated 0 times [  23] [ 7]  / answers: 1 / hits: 34458  / 11 Years ago, tue, july 9, 2013, 12:00:00

I have input field for year and I need a regex for validation it.
I have such code: ^([12]d)?(dd)$.
But I want allow to validate only years in certain range (1990-2010, for example). How can I do it?



Edit. range must be 1950-2050


More From » regex

 Answers
47

Try this:



1990 - 2010:



/^(199d|200d|2010)$/


1950 - 2050:



/^(19[5-9]d|20[0-4]d|2050)$/


Other examples:



1945 - 2013:



/^(194[5-9]|19[5-9]d|200d|201[0-3])$/


1812 - 3048:



/^(181[2-9]|18[2-9]d|19dd|2d{3}|30[0-3]d|304[0-8])$/


Basically, you need to split your range into easy regexable chunks:



1812-3048: 1812-1819 + 1820-1899 + 1900-1999 + 2000-2999 + 3000-3039 + 3040-3048
regex: 181[2-9] 18[2-9]d 19dd 2d{3} 30[0-3]d 304[0-8]

[#77120] Sunday, July 7, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
keric

Total Points: 572
Total Questions: 93
Total Answers: 97

Location: Cyprus
Member since Mon, Oct 24, 2022
2 Years ago
;