views:

50

answers:

2

Hey.

I'm trying to validate a datepicker in a form, but it only seems to work in Firefox 3.6. I can't see what i'm doing wrong, please lend me your wise eyes to check through my code.

Live Example: http://event.gusthlm.se/make.php Try picking any date ("Börjar" and "Slutar"), also try switching month with the datepicker up.

Validation function:

$.validator.addMethod("customDateCheck", function(value, element) {
    var startDateTime = new Date($("#startDate").val()); 
    var endDateTime = new Date($("#endDate").val());
    return (startDateTime) <= (endDateTime);
}, 'Slutdatum kan inte vara innan startdatum.');

$.validator.addMethod("customTimeCheckH", function(value, element) {
    return (((new Date($("#startDate").val())) <= (new Date($("#endDate").val()))) &&
        ((parseInt($("#startTimeH").val(),10) <= parseInt($("#endTimeH").val(),10)))) ||
        ((new Date($("#startDate").val())) < (new Date($("#endDate").val())));
}, 'Sluttid kan inte vara innan starttid.');

$.validator.addMethod("customTimeCheckM", function(value, element) {
    return (((new Date($("#startDate").val())) <= (new Date($("#endDate").val()))) && 
        ((parseInt($("#startTimeH").val(),10) <= parseInt($("#endTimeH").val(),10))) && 
        ((parseInt($("#startTimeM").val(),10) <= parseInt($("#endTimeM").val(),10)))) || 
        ((new Date($("#startDate").val())) < (new Date($("#endDate").val())));
}, 'Sluttid får inte vara innan starttid.');

$.validator.addMethod("telefon", function(value, element) {  
    return this.optional(element) || /^[0-9\-\(\)\ ]+$/i.test(value);  
}, "Du angav ett ogiltigt telefonnummer");

$("#makeEvent").validate({
    debug: true,
    onfocusout: function(element) { $(element).valid(); },
    onkeyup: false,
    focusInvalid: true,
    errorElement: "div",
    errorClass: "formErrorContent",
    errorPlacement: function(error, element) {
        element.parent('td').find('.formError').removeClass('hidden');
        error.appendTo(element.parent('td').find('.formError'));
    },
    invalidHandler: function(form, validator) {
        var errors = validator.numberOfInvalids();
        if (errors) {
            var message = errors == 1
            ? 'Du missade 1 fält. Det har markerats ovan i formuläret.'
            : 'Du missade ' + errors + ' fält. De har markerats ovan i formuläret.';
            $("#formError").removeClass('hidden');
            $("#formError span").html(message);
            $("#formError").show();
        } else {
            $("#formError").hide();
        }
    },
    rules: {
        title: {
            required: true,
            minlength: 2,
            maxlength: 100
        },
        host: {
            required: true,
            minlength: 2,
            maxlength: 100
        },
        cat: "required",
        startDate: {
            required: true,
            date: true
        },
        endDate: {
            required: true,
            date: true,
            customDateCheck: true
        },
        endTimeH: "customTimeCheckH",
        endTimeM: "customTimeCheckM",
        latlng: "required",
        desc: {
            required: true,
            minlength: 10,
            maxlength: 255
        },
        webpage: "url",
        tel: "telefon",
        email: {
            required: true,
            email: true
        },
    },
    messages: {
        title: {
            required: "Var god ange en titel",
            minlength: "Din titel måste vara mer än två tecken lång",
            maxlength: "Din titel får inte vara mer än 100 tecken lång"
        },
        host: {
            required: "Var god ange vem som är värd",
            minlength: "Värdnamnet måste vara mer än två tecken lång",
            maxlength: "Värdnamnet får inte vara mer än 100 tecken lång"
        },
        cat: "Var god välj en kategori",
        startDate: {
            required: "Du måste ange ett startdatum",
            date: "Du måste enge ett datum efter formatet ÅÅÅÅ-MM-DD",
        },
        endDate: {
            required: "Du måste ange ett slutdatum",
            date: "Du måste enge ett datum efter formatet ÅÅÅÅ-MM-DD",
        },
        latlng: "Du måste trycka på \"Uppdatera Karta\" innan du kan fortsätta",
        desc: {
            required: "Var god ange en beskrivning",
            minlength: "Din beskrivning måste vara mer än tio tecken lång",
            maxlength: "Din beskrivning får inte vara mer än 255 tecken lång"
        },
        webpage: "Du får bara ange en giltig url (http://domän.se)",
        email: {
            required: "Var god ange din e-postadress",
            email: "Din e-post måste vara formaterad som namn@domän.se"
        }
    }
});

Date picker:

$.datepicker.setDefaults({
    dateFormat: 'yy-mm-dd',
}); 
$("#startDate").datepicker({
    onSelect: function(dateText, inst) {
        if($("#endDate").val() === undefined) {}
        else {
            if((new Date($("#startDate").val())) > (new Date($("#endDate").val())))
                {$("#endDate").val($("#startDate").val());}
        }
    }
});
$("#endDate").datepicker();

Thank you in advance!

A: 

Pretty please? I've ran the code through JSlint and stared myself blind, I can't see what's wrong :/

cinnak
A: 

haha, I didn't read it on detail but I work with the dates on JavaScript using date-js library: www.datejs.com

Totally recommended

Curro
This is not an answer, this is at best a recommendation (and in the worst case spam, but I won't report it as such, as I think you're trying to give genuine advice).
Marcel Korpel
Thanks for trying to help, but please read the actual question next time
cinnak
Can't believe getting a down vote for giving a recommendation. I did read the question but not the code ¬¬. Anyway....
Curro
Well, I don't want to quarrel over a down vote (after all, it's only a down vote, not your girlfriend who's 'stolen'), but this doesn't try to answer the OP's question at all. You even admit you didn't read the code. These kind of recommendations are fine, but as a comment, not as an answer.
Marcel Korpel