0

Trying to do validations (unique) for add and edit.

I am unable to retrive hidden field value in my validations jquery script.

Following is javascript:

//included : 'jquery.validate.min', -- these files included

validate_branch.js

var validator = $('#form_add_branch').validate({
    errorElement: 'div',
    rules: {
        branch_name: {
            required: true,
            ValidateComma: true,
            remote: BASE_URL_PATH+ "learner/branch/is_branch_exists?branch_id="+$('#branch_id').val()
        }
    },
    messages: {
        branch_name: {
            required: languageArray['branch_name_required'],
            ValidateComma: languageArray['branch_name_comma'],
            remote: languageArray['branch_name_exists']
        }
    }
});


/* Comma validation (Comma not allowed)
 * 
 * @param value String
 * 
 * @return boolean
 * 
 */
jQuery.validator.addMethod("ValidateComma", function(value) {
    var filter = /[,]+/;
    if (filter.test(value))
    {
        return false;
    } else
        return true;
}, languageArray['branch_name_comma']);

This is how we calling validations:

<script>
    $("#btn_add_branch").on("click", function() {  
        $('#form_add_branch').validate();
        if ($('#form_add_branch').valid()) {
            var branch_name = $('#branch_name').val();
            var branch_id = $('#branch_id').val();
            $.post(BASE_URL_PATH + "learner/branch/add_branch", {branch_name: branch_name, branch_id: branch_id}, function(data, status) {
                if (data != "false") {
                    $("#succ_msg").show();
                    $("#err_msg").hide();
                    $('#branch_name').val("");
                    $('#branch_id').val("");
                } else {
                    $("#succ_msg").hide();
                    $("#err_msg").show();
                }


            });
        }
    });

    function edit_branch(branch_id) {
        BRANCH_ID = branch_id;
        $.post(BASE_URL_PATH + "learner/branch/get_branch_name", {branch_id: branch_id}, function(data, status) {
            if (data) {
                $('#branch_name').val(data);
                $('#branch_id').val(branch_id);
            }
        });
    }



</script>

Note : In remote $('#branch_id').val() this comes blank, but at server side it comes. Please guide me in right way. Thanks.

VBMali
  • 1,360
  • 3
  • 19
  • 46

1 Answers1

1

You need to add ignore property in your validate method. By default hidden inputs are not validated by jquery.validate.js and thus you need to override this behavior using

$.validator.setDefaults({ 
  ignore: [] //do not ignore any input fields even though its hidden
});

The above method sets that behavior for all the elements irrespective of which forms you have validation. Now if you want to ignore just one field, then you can do it as below:

var validator = $('#form_add_branch').validate({
      ignore: ":hidden:not('#branch_id')" //ignore all hidden fields except #branch_id
});
Guruprasad J Rao
  • 29,410
  • 14
  • 101
  • 200