javascript regex validate years in range
For a range from 1950 to 2050 you may use the following regex:
^19[5-9]\d|20[0-4]\d|2050$
Online demo
RegExp does not seem to be the right tool here. If you have the year values already isolated surely a simple comparison would work :
if (+yr >= 1990 && +yr <= 2010)
The +yr
converts the string to a number
Try this:
1990 - 2010:
/^(199\d|200\d|2010)$/
1950 - 2050:
/^(19[5-9]\d|20[0-4]\d|2050)$/
Other examples:
1945 - 2013:
/^(194[5-9]|19[5-9]\d|200\d|201[0-3])$/
1812 - 3048:
/^(181[2-9]|18[2-9]\d|19\d\d|2\d{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 19\d\d 2\d{3} 30[0-3]\d 304[0-8]
Regex:
/^(19[5-9]\d|20[0-4]\d|2050)$/
Easier...
var year = parseInt(textField.value, 10);
if( year >= 1950 && year <= 2050 ) {
...
}