0

I have a custom validation rule for jQuery Validate. It should prevent the user from inserting a passenger number greater than a vehicle's available seats. The message I launch pop's out if the inserted value is wrong, but then the validation doesn't prevent me from submitting the form. What's the problem?

Here's the code:

$(function()
    {

        $.validator.addMethod("maxpass", function(value, element) {
                var del = "${requestScope.delete}";
                if (del == "true") {
                    return true;
                }
                var ret = "${requestScope.returnitinerary}";
                if (ret == "true") {
                    return true;
                }
                sum = 0;
                $("#vehicles :selected").each(function() {
                    $.ajax({
                        url : "${pageContext.request.contextPath}/vehicle/seatsno",
                        data : "id=" + $(this).val(),
                        async : false,
                        success : function(resp) {
                            sum += Number(resp);
                        }
                    });
                })
                 return (sum >= Number($("#passengersNo").val())) ;

            }, "Numero posti insufficiente!"),


            $("#itinerary").validate(
            {

                //validation rules
                rules :
                    {
                        departureDate : "required",

                        departureTime : "required",

                        arrivalDate : "required",

                        arrivalTime : "required",

                        arrivalCity : "required",
                        passengersNo :
                            {
                                maxpass : true,
                                required : true,
                                number : true
                            },
                        drivers :
                            {
                                required : true,
                                drive : true
                            },
                        vehicles : "required",
                        departureCity : "required",
                        serviceCost : "number",
                        transitPassCost : "number"
                    },

                //error messages
                messages :
                    {
                        vehicles : "Numero autisti non valido",
                    },

                submitHandler : function(form)
                    {
                        form.submit();
                    }
            });

    });
Sparky
  • 98,165
  • 25
  • 199
  • 285
  • `"${requestScope.delete}"` is not JavaScript, and how are we supposed to know what the `del` variable represents? Please only show us the ***rendered*** JavaScript and the **relevant** rendered HTML markup of the form. Thanks. – Sparky Mar 02 '16 at 15:31
  • [It's not a good idea to use Allman style code formatting for JavaScript](http://stackoverflow.com/a/11247362/594235). – Sparky Mar 02 '16 at 15:33
  • *"...doesn't prevent me from submitting the form"* ~ This is very unlikely... if you see a validation error message (*from this plugin*), then you will not be able to submit the form. You also should not be doing `.ajax()` within your custom method. Use [the `remote` rule](http://jqueryvalidation.org/remote-method/) instead of reinventing the wheel. – Sparky Mar 02 '16 at 15:36
  • sorry about "${requestScope.delete}" it's some spring expression language... and thank you about the info on js formatting style. I'm new to JS so i know little about it. As for the unability of submitting the form after the validation, the problem was another JS script on that page... Thanks fot the remote rule too! – Alexander C. Mar 08 '16 at 09:20

0 Answers0