2

I have two hidden input fields on my form that I have linked to jquery validation rules. These are sort of "global" form rules (for example the total of multiple fields not equal to 100%), so I am using hidden fields at the top of my form to show a message that applies to the whole form.

They are defined like this:

<input type="hidden" class="futuretotalsunder"/>
<input type="hidden" class="futuretotalsrequired"/>

Here are my rule definitions. You can get the idea of it. The first rule checks to see if a bunch of text boxes total up to < 100, and the second check to make sure the total isn't 0. The problem is, the only one that fires is whichever one is applies to the first hidden input element. The second one never fires. Any idea why?

$.validator.addMethod("futuretotalsunder", function (val, element, params) {
        alert("under");
        var total = 0;

        $(".TransferPercentTextBox").each(function () {
            total = total + parseInt($(this).val());
        });

        if ((total < 100) && (total != 0)) {
            window.location.href = "#Top";
            return false;
        }
        else {
            return true;
        }
    }, "Total percentage must equal 100%.");

     $.validator.addMethod("futuretotalsrequired", function (val, element, params) {

        var total = 0;

        $(".TransferPercentTextBox").each(function () {
            total = total + parseInt($(this).val());
        });

        if (total == 0) {
            window.location.href = "#Top";
            return false;
        }
        else {
            return true;
        }
    }, "Transfer amount must be greater than 0.");
2
  • Is your problem related to asp.net or mvc in any way? You tagged it as such (presumably that's your back end tech), but it looks like your problem is on the front end JS only. Is there some interaction you left out?
    – Patrick M
    May 1, 2012 at 19:27
  • Yeah that was my back end technology. (asp.net MVC) but I wasn't sure how to tag it. I figured it out and will answer below.
    – jeff.eynon
    May 1, 2012 at 19:32

1 Answer 1

4

I figured it out. The problem (oddly) was that my input elements didn't have name attributes. Once I added them it worked fine.

2
  • +1 for adding the name attribute. Fixed a similar issue for me too. May 9, 2012 at 9:26
  • In my case I also had two input fields with no names. Curiously one works with no name, but the second one needs a name. So now I have input #1 does not have a name and input #2 has a name. Feb 19, 2015 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.