0

Like SO's question or answer form alert message: Body must be at least 30 characters; you entered 3., the last number display the current amount of characters. For that I think that I need calculate the number and append the error message, and I can append the number to the errorPlacement's first parameter error like this: http://jsfiddle.net/sscs1kbx/5/

errorPlacement: function(error, element) {
  var placement = $(element).data('error');
  if (placement) {
     error.append(CKEDITOR.instances.editor1.getData().replace(/<[^>]*>/g, '').length);
    $(placement).append(error)
  } else {
    error.insertAfter(element);
  }
}

I works fine when user click submit button first time, but errorPlacement is called only once at the first time submitting, so, after that, I works failed? which method or function should I use for this goal? Thanks a lot in advance!

Sparky
  • 98,165
  • 25
  • 199
  • 285
abelard2008
  • 1,984
  • 1
  • 20
  • 35
  • Please do not tag-spam. The jQuery Validation Engine and the jQuery Validate plugin are not the same thing. Edited. Thanks. – Sparky May 18 '15 at 14:40

1 Answers1

1

You can specify a dynamic error message in your addMethod("minlengthxo",...):

jQuery.validator.addMethod("minlengthxo", function (value, element, param) {
    debugger
    originalVal = CKEDITOR.instances.editor1.getData().replace(/<[^>]*>/g, '');
    var length = $.isArray(originalVal) ? value.length : this.getLength(originalVal, element);
    return length >= param;
}, function (params, element) {
    enteredLength = CKEDITOR.instances.editor1.getData().replace(/<[^>]*>/g, '').length;
    return 'You need input at least 18 characters, now you entered ' + enteredLength
});

Here is a live exemple

EdenSource
  • 3,387
  • 16
  • 29
  • Thank you, this is the answer I have be looking for! I tried this: `var errMsg = function() {...};` and used this as the third parameter, `jQuery.validator.addMethod("minlengthxo", function(){}, errMsg)`, but it failed, I don't know why? Here, defining a anonymous function is must? – abelard2008 May 18 '15 at 10:44
  • Thanks , what's the difference between your way and mine? This must be a javascript syntax question, sorry – abelard2008 May 18 '15 at 10:56
  • This is a good question which you can find an answer here: http://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname – EdenSource May 18 '15 at 11:08
  • Thanks a lot! I also found this question. – abelard2008 May 18 '15 at 11:11