Here is an example I found
Here is an example I found
sorry Idont know how to adapt it to: YYY-MM-DD format, which they need to type into a text input box
Rees
Have you tried using the PHP strtotime function?
What are they typing in?
What are you comparing it to?
js compares a full sql date
I learned me some js regEx's /biggrin.gif" style="vertical-align:middle" emoid=":D" border="0" alt="biggrin.gif" />
Try this code in your form:
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>
Sorry, but I forgot to add the ';' after process = 'yes'.
Don't forget that.
what does the \1 do?
does that tell it to repeat the first group? eg. -> (\-|\/|\.) ?
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.
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.
the original regex i wrote was for js, not php....
and it should work just fine matching a full sql date
That's supposed to be YYYY-MM-DD, right?
$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.';
}
?>
D'ya want another one. /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" />
(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
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
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