I want to validate when date and time selected is past current date and time

I have two readonly textboxes, one textbox is linked with the jquery datepicker and the other textbox is linked with the timepicker jquery. Now the great thing about the jquery datepicker is that I can range my dates so that the user cannot select any dates before the current date. But the Jquery timepicker cannot be ranged so the time is before the current time.

Because of this there is a possbile chance that the user can select the current date but a time before the current time. Obviously this means the time chosen has passed the current time of the current day which I don’t want happening. So a validation is needed where if date textbox and time textbox is past the current date and time, then a message is displayed stating “The Date and Time you have selected is before the Current Date and Time” else if time and date together is past current date and time then display “”.

How can I do this. Below is my best attempt at it but I couldn’t get it to work.

                <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
                <title>Create a Session</title>
                        <script type="text/javascript">

         function validation() {
            			var dateTextO = document.getElementById("datepicker");
            			var timeTextO = document.getElementById("timepicker");
            			var errDateTimeMsgO = document.getElementById("dateTimeAlert")

        var currentDate = new Date()
        	var day = currentDate.getDate()
        	var month = currentDate.getMonth() + 1
        	var year = currentDate.getFullYear()
        	var currentTime = new Date()
        	var hours = currentTime.getHours()
        	var minutes = currentTime.getMinutes()
        	if (minutes < 10){
        	minutes = "0" + minutes
        	if (hours < 10){
        	hours = "0" + hours
        	if((dateTextO < currentDate) && (timeTextO < currentTime)){
        		errDateTimeMsgO.innerHTML = "The Date and Time you have selected is before the Current Date and Time";
        	} else {
        		errDateTimeMsgO.innerHTML = "";


        <form action="create_session.php" method="post" name="sessionform">
        <p><strong>4: Date:</strong> <input type="text" id="datepicker" readonly="readonly"></p>
                    <p><strong>5: Time:</strong> <input type="text" id="timepicker" readonly="readonly"><span class="timepicker_button_trigger"><img src="Images/clock.gif" alt="Decrease" /></span></p>
                    <div id="dateTimeAlert"></div>

Are you sure it’s your attempt? At codingforums I saw you claim the spinner code I posted here as your code :lol:. You’re not trying to get someone here to fix code you have got from somewhere else are you? :smiley:

In any case, you need to specify which current time you mean - server time or the user’s local time on their pc?

And what about users with browsers where javascript is either disabled or not available? I assume you are aware this really needs to be done server side to eliminate the limitations of javascript.

No honestly this is my attempt on it but I can’t get it to work. This application is only for universities only, not for any user so as long as universities have javascript (which I am guessing they all will), then there should be no probs. Plus I am using some Jquery functions so need to include javascript

The error console in whatever browser you are using must be spitting out some error messages because you have a few I can see.

What error messages are you getting? From the error messages you should be able to at least work out what line(s) has an error.

To debug your code, either use the debugger in your browser or just use alert() statements to check the value of variables at various points in your code. Where an alert() shows a problem, back track from there to fix the problem

If you are not including warnings then only errors I’m getting is a image corrupt error which is from an unknow url and a this.elem null error is the only error I have got from my document. A lot of warnings are from the jqueries I have included.

The error message should tell you the line number as well.

The above error is telling you that this.elem either doesn’t exist when the line of code is executed or it does exist but is set to null. If you wrote all that code then you know which element in your html this.elem is referring to so you should be able to step through your code to work out why it is null. Using alert() statements should help you debug your code if you’re not using a debugger.

Hi, it is not coming up with that error any more, but can’t get the date and time validation working

then you must’ve changed something in your code. If you’re not getting any errors then that means you have only logic errors in your code. Just continue stepping through your code using a debugger or alert() statements checking values of variables at various points and where variable values are not correct, back track your code to work out why.

Think of debugging as character building :slight_smile:

If you don’t develop your own debugging skills then you’re going to lose a lot of time in the future waiting for someone in forums to help you.