Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validating dates with Reg. Exp.'s

Status
Not open for further replies.

beirti

Programmer
Joined
Jun 18, 2003
Messages
18
Location
IE
Im pretty new to using regular expressions - up to now ive never had a problem but I need to validate a date on my form for use in oracle(DD-MMM-YYYY or DD-MMM-YY).
My first attempt goes a little something like this...

if (!eregi("^
( (([0-2]{1})([0-9]{1})) | (([3]{1})([0-1]{1})) )
(-[JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC])
(-[0-9]{2,4}) +$"))

The idea is to
1. ensure first number is 00-29 | 30-31
2. find a hyphen then JAN-DEC
3. ensure last number is 0000-9999

I've an idea that this is way off, so would appreciate any help
 
beirti,

Here's the easy solution:
split your input fields in the form into the three units you are looking for and assemble them later. By using <select> form elements that only contain valid entries you can skip the validation as there will always be a valid date selected.
Example (in the form):
Code:
<select name=&quot;month&quot;>
<option value=&quot;JAN&quot;>January</option>
<option value=&quot;FEB&quot;>February</option>
etc.
 
I could also recommend that you split the date at the dashes into an array (preg_split() is one function that you could use), then deal with each part separately.

This will allow you to also perform cross-column data validation. Like the appropriate maximum number of days for February.

Want the best answers? Ask the best questions: TANSTAAFL!
 
I didn't want to use the combo boxes as it can be annoying at the best of times. Also, with combo boxes, i need to use javascript to ensure that the correct number of days is available for the selected month, not to mention leap years. I sis eactly that in a Java project and ended up with a lot of code. Thanks for the ideas though
 
A regular expression will not suffice to achieve checks you mention in your last post. You will still have to do exactly the same date evaluation you would have to do on combo boxes.

Number of days/month and leap years have nothing to do with the combo boxes. It is just a question of presentation and interface. I disagree that using <select> menus requires any more code than a single text field.
 
If you want it done so that you can't select a startdate as February 31st and an enddate in January of the previous year you need extra code. All of this needs to be done before the date is entered, otherwise it just leads to more validation code
 
You need the same code when you use a texfield. Just as sleipnir said, nothing prevents the user from entering Feb 31st into that field either.

I also think that validation is best handled in PHP as you cannot depend upon the browser to execute JavaScript - people still turn it off and you have to reckon that. Good coding needs to take the worst case scenario into consideration. You just can't expect the user/browser to provide valid dates, validation of all the possible mistakes is necessary.

If you want to do it client side, JavaScript is a good option when it works, when it doesn't the PHP still has to weed out the foul data.
 
For anyone interested, I worked out the ereg code. It's a bit easier to just take it in as 01-01-2003 instead of 01-JAN-2003 and convert the month to letters for use in oracle. Came across another interesting bug in php while doing this though.

If you call a value in an array e.g. $myArray[1] it gives you a result. However, if you use a variable to index the array e.g. $myArray[$myIndex]] it returns null.

Any ideas out there??
 
Verify that $myIndex contains the value you think it does.

Also, how are you outputting $myArray[$myIndex]? Some versions of PHP got confused if an array reference using a variable index was used inside quotation marks, for example. Does
Code:
print $myArray[$myIndex];
work?


Want the best answers? Ask the best questions: TANSTAAFL!
 
Figured it out.

Php doesn't have any types for variables.

You need to do this:

$dayArray[(int)$tempDate[2]];

I was just writing a function to validate the dates - its pretty simple. I'll post it if anyone wants
 
Its a habit of mine from JAVA. only started PHP bout a month ago. Have the code here for the date validation function:

it takes in a date in the format dd-mm-yy

function isDateValid($strDate)
{
$dayArray=array(0,31,28,31,30,31,30,31,31,30,31,30,31);

if (ereg('([0-9]{1,2})-([0-9]{1,2})-([0-9]{2}|[0-9]{4})',$strDate, $tempDate))
{
$tempYear = (int)$tempDate[3];

if (($tempYear%4==0 && $tempYear%100!=0 )|| ($tempYear%400==0))
{
$dayArray[2] = 29;

}// End of If

if ($tempDate[1] > $dayArray[(int)$tempDate[2]] || $tempDate[1] < 1)
{
echo &quot;Invalid Date<br>&quot;;
exit;

}// End of If

switch($tempDate[2])
{
case 1: $tempDate[2] = &quot;JAN&quot;; break;
case 2: $tempDate[2] = &quot;FEB&quot;; break;
case 3: $tempDate[2] = &quot;MAR&quot;; break;
case 4: $tempDate[2] = &quot;APR&quot;; break;
case 5: $tempDate[2] = &quot;MAY&quot;; break;
case 6: $tempDate[2] = &quot;JUN&quot;; break;
case 7: $tempDate[2] = &quot;JUL&quot;; break;
case 8: $tempDate[2] = &quot;AUG&quot;; break;
case 9: $tempDate[2] = &quot;SEP&quot;; break;
case 10: $tempDate[2] = &quot;OCT&quot;; break;
case 11: $tempDate[2] = &quot;NOV&quot;; break;
case 12: $tempDate[2] = &quot;DEC&quot;; break;
default: echo &quot;Invalid Month<br>&quot;; exit; break;

}// End of Switch

$newDate = $tempDate[1];
$newDate .= &quot;-&quot;;
$newDate .= $tempDate[2];
$newDate .= &quot;-&quot;;
$newDate .= $tempDate[3];

return $newDate;

}// End of If

else
{
return -1;

}// End of Else

}// End of isDateValid()
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top