0

I have a custom validator that points to a Client side script to validate a textbox.

My date and time are separated into two textboxes - one with date in mm/dd/yyyy format and the other with time in hh:mm am/pm format.

I need to make sure the textboxes together are not greater than now. How can i accomplish this?

Here is what i have so far. what am i doing wrong?

function checkminutes(sender, args) {
        var txtdate = $get('<%=FormView1.FindControl("txtdate").ClientID %>');
        var txttime = $get('<%=FormView1.FindControl("txttime").ClientID %>');
        var totaltime = txtdate.value + ' ' + txttime.value;
        totaltime = Date(totaltime);
        var d = new Date();
        if (totaltime > d) {
            args.IsValid = false;
        }
        else {
            args.IsValid = true;
        }


    }

This is the answer that worked.

function checkminutes(sender, args) {
        var txtdate = $get('<%=FormView1.FindControl("txtdate").ClientID %>');
        var txttime = $get('<%=FormView1.FindControl("txttime").ClientID %>');
        var totaltime = txtdate.value + ' ' + txttime.value;
        totaltime = Date.parse(totaltime);
        var d = new Date();
        if (totaltime > d) {
            args.IsValid = false;
        }
        else {
            args.IsValid = true;
        }
    }
1
  • 1
    I feel like I've said this a million times. Make sure that you validate server-side, as any client-side validation can be circumvented. I used to get around twitter's 140 character limit that way. Commented Oct 29, 2009 at 15:27

3 Answers 3

1

Just compare the milliseconds since the epoch:

totaltime = new Date("1988/02/21 08:08");
d = new Date();
if (totaltime.getTime() < d.getTime())
    alert("Date is valid");
else
    alert("Try again, Date is not valid");

EDIT: I can't seem to get it to work when I use "am/pm", so just convert it to 24 time, and it will be fine.

Sign up to request clarification or add additional context in comments.

Comments

0

Don't waste your time validating this on the client-side. It's never going to be reliable, and no matter what you are going to have to validate on the server-side anyway.

4 Comments

But you cannot get the exact time on server side validation. Unless the page is refreshed or i use updatepanels.
no, validate client-side so the user gets immediate feedback that something is wrong. then validate server-side to make sure that it wasn't circumvented in a possibly malicious attempt.
i like your answer geowa4. But what is my issue in the above code?
@geiwa4- i agree, thats what validating on client side is for, immediate feedback, but you were saying its a waste of time which is why I disagreed
0

You shouldn't be using javascript popups to alert the user there was a problems; they're clunky at best.

Unless you're telling the user there was a problem with AJAXy pages, just do server side validation, and your application will be slicker looking. I'm assuming if you're asking this particular question, you're not doing AJAX.

As far as serverside validation not having "the exact time", you're dealing with hours and minutes, not milliseconds.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.