I’m new to java script. I want to validate wether the time selected from drop down list is past the current time.
For example:
The user selects 2:00pm while the current time is 1:00pm, now the following error should popup:
The time you have selected is before the current time.
How can I do this? Below is my drop down list.
<?php
$start = strtotime('12:00am');
$end = strtotime('11:30pm');
echo '<select name="time">';
for ($i = $start; $i <= $end; $i += 1800)
{
echo '<option>' . date('g:i a', $i);
}
echo '</select>';
?>
I have tried the following however I could not make this working.
<script type="text/javascript" language=JavaScript>
function validate_time(elmnt)
{
var date_in = new date(elmnt);
var date_now = new date(); //returns current date by default;
if( date_in<date_now ){ alert('The time you have selected is before the current time.');
}
</script>
</head>
<body>
<div align="left">
<?php
$start = strtotime('12:00am');
$end = strtotime('11:30pm');
echo '<select name="time">';
for ($i = $start; $i <= $end; $i += 1800)
{
echo '<option>' . date('g:i a', $i);
}
echo '<select name=time onchange="OnChange(this.form.select)">';
echo '</select>';
?>
Write an event handler function for the onchange() property of the select element. Put it in a < script > tag in the header section of your document. Then attach it to your select element. Something like this: