0

I am stuck in one scenario. I have a field text 2 which is required as well as with text3 it should be adding up to 100%. The problem is it gets validate invidually but I want the validations to be shown together. like

  1. Text2 is required

  2. Sum of Text2 and Text3 must equal 100%

Below is my code

<form action="" id="frmLogin" name="frmLogin" method="post">
 <input type="text" id="text1" name="text1" placeholder="text1">
 <input type="text" id="text2" name="text2" placeholder="text2">
 <input type="text" id="text3" name="text3" placeholder="text3">
 <input type="submit" id="submit" value="Submit" data-mini="true">
</form>

$('#frmLogin').validate({
onfocusout: false,
onkeyup: false,
ignore: "",
rules: {
    text1: {
        required: true
    },
    text2: {
        required: true,
        TotalSum: true
    }
},
showErrors: function (errorMap, errorList) {
    var messages = "";
    var check = 0;
    $.each(errorList, function (index, value) {
        check = 1;
        var id = $(value.element).attr('id');
        messages += (index + 1) + ". " + value.message + "\n";
    });

    messages = "Please correct following errors \n" + messages;
    if (check == 1) {
        alert(messages);
    }
},
submitHandler: function () {
    alert('Success!!!');
},
messages: {
    text1: {
        required: "Please enter text1"
    },
    text2: {
        required: "Please enter text2"
    }
}
});

jQuery.validator.addMethod("TotalSum", function () {

var arr = ['text2', 'text3'];
var Sum = 0;
for (var i = 0; i < arr.length; i++) {
    var test = arr[i];
    var sum2 = $('#' + test).val();
    if (sum2 == null || sum2 == 'NaN' || sum2 == '') sum2 = 0;

    Sum = parseInt(sum2) + parseInt(Sum);
}
if (Sum != 100) {
    return false;
} else return true;
}, "Sum of Text2 and Text3 must equal 100%");

Here is the Fiddle for the same FIDDLE

Sparky
  • 98,165
  • 25
  • 199
  • 285
Rohit Tiwari
  • 822
  • 5
  • 16
  • possible duplicate of [jQuery validate across multiple fields](http://stackoverflow.com/questions/18604098/jquery-validate-across-multiple-fields) – Sparky Jan 13 '14 at 16:26
  • Also see my answer on this question: http://stackoverflow.com/questions/20918702/validating-three-text-fields-with-jquery-validate-plugin/20923357#20923357 – Sparky Jan 14 '14 at 00:58

1 Answers1

0

I can't make sense of your description... where you're talking about field #2 and #3 but never mention what's going on with your field #1. Anyway, this answer is pretty close in that it validates two fields to add up to 100 and places the error message in another location. I'm sure you could easily tweak it to suite your needs.

Working DEMO: http://jsfiddle.net/B6N5f/

$(document).ready(function () {

    jQuery.validator.addMethod("TotalSum", function (value, element, params) {
        total = parseFloat(value) + parseFloat($(params[0]).val());
        return total == 100;
    }, "Total must add up to 100%!");

    $("#frmLogin").validate({
        rules: {
            text1: {
                required: true,
                TotalSum: ['#text2']
            },
            text2: {
                required: true,
                TotalSum: ['#text1']
            }
        },
        groups: {
            percent: "text1 text2"
        },
        errorPlacement: function (error, element) {
            error.insertAfter($('#submit'));
        }
    });

});
Sparky
  • 98,165
  • 25
  • 199
  • 285
  • In your sample 1st when I click submit it will show fields are required and then when i enter text it will show sum should be 100,which even I am able to do in my fiddle but all I want is these two messages to be shown together when I click submit messages should be like #field2 is required and sum of 2 and 3 should be 100 – Rohit Tiwari Jan 14 '14 at 07:22
  • @RohitTiwari, then [read the documentation](http://jqueryvalidation.org/validate). Example code is shown there for doing all of that. Otherwise, fix your question so that it makes more sense. – Sparky Jan 14 '14 at 15:18