Error message in jquery form-validator

701 views Asked by At

I am using this library for form validation: https://github.com/victorjonsson/jQuery-Form-Validator/

I am making a custom controller using the code below:

var errMsg = "";
// Add custom validation rule
$.formUtils.addValidator({
  name: 'even_number',
  validatorFunction: function(value, $el, config, language, $form) {
    if (cond1) {
      errMsg = "something1";
      flag = false;
    } else if (cond2) {
      errMsg = "something2";
      flag = false;
    }
    console.log(errMsg);
    return flag;
  },
  errorMessage: errMsg,
  errorMessageKey: 'badEvenNumber'
});

// Setup form validation
$.validate();

The field is shown as red - as it should - on the validation failure. But, it doesn't show the message. If I give default string to errMsg then it shows the given static string.

1

There are 1 answers

0
Ashish Joshi On

The library itself doesn't provide this feature but i found a work around.

I changed the data-validation-error-msg attribute on the fly in the conditions and it worked.

Though thanks everybody.