delphi programming forums mysql charset mget recursive synonimos
free ventrilo servers hosting cs javascript delay python find in list
Back Forum New
abstract:

"/^{1,2}{1,2}{4}$/"
Does this look right?
It's supposed to match mm/dd/yyyy date format


I'm trying to tweak a piece of javascript that defines a regular expression for date formatting but I'm not sure if mine is correct. This is what I have:
"/^[0-9]{1,2}[/][0-9]{1,2}[/][0-9]{4}$/"
Does this look right?
It's supposed to match mm/dd/yyyy date format

TOP

Advice: try it. You probably have by now. It's a bit too general. 00/00/0000 will return a true match, so will 99/99/9999. However, they're obviously not correct dates. You also don't need the square brackets around the forward slashes--escape them instead (or in the case of the code below, accept any character to delimit the date).
Code:
  1. /^(0[1-9]{1}|[12]{1}[0-9]{1}|3[01]{1}).{1}(0[1-9]{1}|1[0-2]{1}).{1}([12]{1}[0-9]{3})$/
Copy Code
This will accept 01-10-2009, 01/10/2009, or any other delimit character chosen. If you want it to be exactly the forward slash, replace the two parts where it's written .{1} with \/
Just beware: If you plan to verify dates before the 11th century or after the 30th century, you'll need to modify it.

TOP

Yeah--clarification. The more he knows of its workings, the sooner he'll be able to skip the longhand--kind of like math.

TOP

Since your entire string is a date, wouldn't it be easier (and more accurate) to say !isNaN( Date.parse( input_string ) )?
If you absolutely must have it in MM/DD/YYYY form you can always format the result of Date.parse.



"/^{1,2}{1,2}{4}$/"
Does this look right?
It's supposed to match mm/dd/yyyy date format

TOP

Back Forum