Allow Event To Proceed

I have a custom date picker jQuery plugin. On form submission, the date picker validates the selected date to check whether it is valid or not. In case it is not, it displays an error message, otherwise it should proceed and post the form.

What happens is that in case the user inputs a valid date on the first time, everything works fine. But, in case the user inputs an invalid date, the error message shows up and then when the user fixes the date, it still doesn't post the form. It just sits there.

Here's the part of the code that handles the form submit event:

        var hiddenInput = $('.custom-date-picker-value');
        var form = hiddenInput.closest('form');
        $(form).submit(function (e) {
            selectedMonth = monthSelect.find('option:selected').text();
            selectedYear = yearSelect.find('option:selected').text();
            selectedDay = daySelect.find('option:selected').text();

            $('#dateValidationMessage').html('');
            if (selectedMonth == '' || selectedDay == '' || selectedYear == '') {
                $('#dateValidationMessage').html($('#dateOfBirthRequired').val());
                e.preventDefault();
            }
            else {
                var dateValue = selectedMonth + '/' + selectedDay + '/' + selectedYear;
                var dob = new Date(dateValue);
                var modifiedDate = new Date(dob.getFullYear() + 2, dob.getMonth(), dob.getDay());
                if (modifiedDate > new Date()) {
                    $('#dateValidationMessage').html($('#AgeGreaterThan').val());
                    e.preventDefault();
                }
                else {
                    hiddenInput.val(dateValue);
                    console.log('date is valid');
                }
            }
        });

When the user inputs a valid date, I can see a trace being output to the console saying "date is valid", but for some reason the form does not get submitted.

What's happening here?

Answers


Try to

return true 

in this case,

console.log //returns undefined. 

Also, there's a bug with preventDefault on form submit Events in some browsers, so you might want to change

e.preventDefault() 

to

return false;

Need Your Help

Failed to load resource: net::ERR_FILE_NOT_FOUND loading json.js

javascript jquery html json

I am testing an html webpage and it is failing to load a local jquery.json-2.4.0.js. I am testing the html page locally from chrome. When the page loaded I get a net::ERR_FILE_NOT_FOUND.

Eclipse Errors - FRC - Windows

c++ gcc plugins error-handling g++

I am having some issues. I am trying to run some FRC plugins in Eclipse. This program executed is a test program. My goal is to be able to deploy but it does not seem to work.