0

I am trying to validate a textarea (CK Editor) using jQuery validation and it is not working in button click event. Whereas it is working if called separately.

Fiddler: http://jsfiddle.net/BmZ93/453/

JS:

$(document).ready(function () {
     var FormAllClass = '.form-all'; 

        var IsValid = function () {
          $(FormAllClass).validate();
          $('#desc').rules("add", {
              required: true,
              messages: {
                  required: "Description is required."
              }
          });

          $(FormAllClass).valid();
    };

    //IsValid() //Working 


    $('#job-submit').on('click', function(){

            if (IsValid())//not working
        {

        }
    })
});

In the code above, I have marked where it is working and where not.

Any suggestion on how to fix this will be greatly appreciated.

JGV
  • 5,037
  • 9
  • 50
  • 94
  • Hint: the `.validate()` method is only used for initialization, so it's called once on DOM ready, not `click`. – Sparky Nov 15 '16 at 00:29

1 Answers1

0

The actual text area seems to be hidden. jQuery validation seems to ignore hidden element by default. So, to fix the issue, set the ignore parameter to empty string in validate method.

This will not ignore the hidden element,

$(document).ready(function () {
     var FormAllClass = '.form-all'; 

        var IsValid = function () {

          $(FormAllClass).validate({
               ignore: ''//do not ignore hidden elements
           });

          $('#desc').rules("add", {
              required: true,
              messages: {
                  required: "Description is required."
              }
          });

          $(FormAllClass).valid();
    };   


    $('#job-submit').on('click', function(){

        if (IsValid())//not working
        {

        }
    })
});
JGV
  • 5,037
  • 9
  • 50
  • 94
  • Technically, as per the developer, it should be `ignore: []` – Sparky Nov 16 '16 at 16:28
  • I am using typescript and the typing indicates that the ignore property is a string and not an array. So, I am not able to set []. – JGV Nov 16 '16 at 17:42
  • Then that is a little problem with typescript. See: http://stackoverflow.com/a/8565769/594235 – Sparky Nov 16 '16 at 18:09