Welcome to the Geeks & God Static Archive. Read more »

Date Validation

Joined: 11/28/2008

Hiya I am doing some validation on a form, and I was wondering how I compare a date ina input box to the format I want. I know this is possible but dont know how it is done, can any1 help?

Thanks
Rees

Joined: 11/28/2008
CODE EDIT: was not helpful,
CODE
EDIT: was not helpful, so removed for ease of scrolling through posts.

Here is an example I found

TJ SINGLETON

Joined: 11/28/2008
sorry Idont know how to adapt

sorry Idont know how to adapt it to: YYY-MM-DD format, which they need to type into a text input box

Rees

Joined: 11/28/2008
Have you tried using the PHP

Have you tried using the PHP strtotime function?

Paul Davey
Whitford Church
"Everyone who calls on the name of the Lord will be saved." Romans 10:13
"For all have sinned and fall short of the glory of God, and are justified

Joined: 11/28/2008
What are they typing in?What

What are they typing in?
What are you comparing it to?

If you liked this post, you'll love The Open Source Ministry Forum
And, the Open Source Ministry Blog {

Joined: 11/28/2008
js compares a full sql

js compares a full sql date

CODE
var datePattern = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/;

I learned me some js regEx's /biggrin.gif" style="vertical-align:middle" emoid=":D" border="0" alt="biggrin.gif" />

Joined: 11/28/2008
Try this code in your

Try this code in your form:

CODE
<script type="text/javascript">

function validateForm(){

var process = "yes"

var dateFilter =  /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
if(document.yourForm.myDate.value == "" || !dateFilter.test(document.yourForm.myDate.value)){
alert('The date you entered is invalid');
process = "no"
} // end of if

if(process == "yes"){
document.yourForm.submit();
} // end of if

} // end of function validateForm

</script>

<form name="yourForm" method="yourMethod" action="yourAction">

Your form content

<input type="text" name="myDate" value="" /> (mm/dd/yyyy)
<input type="button" name="submit" onclick="validateForm" />

</form>

Joined: 11/28/2008
Sorry, but I forgot to add

Sorry, but I forgot to add the ';' after process = 'yes'.

Don't forget that.

Joined: 11/28/2008
what does the \1 do?does that

what does the \1 do?
does that tell it to repeat the first group? eg. -> (\-|\/|\.) ?

Joined: 11/28/2008
That actually wasn't the best

That actually wasn't the best way to write that expression. Upon looking back at it, I'm not sure why I did it that way. It will work, but to make things a little less confusing, use the following expression:

(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d

This one might not be as complicated.

Joined: 11/28/2008
I should never post during

I should never post during prime time... that last expression wouldn't work well with the javascript... you should still use /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/. The other one would be used better in php or perl.

Sorry. My Bad.

Joined: 11/28/2008
the original regex i wrote

the original regex i wrote was for js, not php....
and it should work just fine matching a full sql date

Joined: 11/28/2008
That's supposed to be

That's supposed to be YYYY-MM-DD, right?

CODE
<?php

$date = '2005-03-09';

if (!preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $date))
{
 echo 'Please enter a date in the format: YYYY-MM-DD.';
}

?>

bob
bob's picture
Joined: 11/28/2008
D'ya want another one.

D'ya want another one. /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" />

CODE
^[0-9]{4}-(((0[13578]|(10|12))-
(0[1-9]|[1-2][0-9]|3[0-1]))|
(02-(0[1-9]|[1-2][0-9]))|
((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))$

(Should all be one line) Matches the date format "YYYY-mm-dd" and also validates month and number of days in a month. All leap year dates "YYYY-02-29" passes trough. Could easily be changed to another format.
ref: RegExLib

Joined: 11/28/2008
Thank you so much every1

Thank you so much every1 /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /> I am glad you knew what I ment. It works /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /> yepi

Thanks again
Rees