AussieBattler10
Canvas Initiate

SP 2013 - Limit a ML TextBox entry to 250 words

Hi Guys

 

I am new to using Javascript with Nintex for SP2013 and I was wondering if someone could help me out with this. Lets say I have a multi line textbox called 'projsumm' and I want to restrict the number of words to be put into this entry to 250 words. I had found this code on a previous thread and was wondering if i could use it in some way although I am unsure as to how to reference the actual textbox 'projsumm' in this particular case.

 

function wordCount(source, args) {

var words = args.Value.split(” “);
words = NWF$.grep(words, function (element, index) {
if (element.trim() != “”) {
return true;
}
});
if (words.length < 20)
{
args.IsValid = false;
}
}

 

Any help would be greatly appreciated or providing alternative methods of how to achieve what I am after.

 

Cheers,

 

0 Kudos
Reply
3 Replies
Tarf
Design Canvas Artiste

Re: SP 2013 - Limit a ML TextBox entry to 250 words

Hi @AussieBattler10,

for a text-only multiline textbox I got this working:

 

NWF$("#" + multibox).val().split(" ").length

 

this assumes that there will not be any double/tripple spaces.

For use with multiple spaces, this might work:

 

NWF$("#" + multibox).val().split(" ").reduce(function (sum, current) {
  return sum += current.trim() != "";
}, 0);

 

EDIT: Or this if you do not want to use reduce

NWF$("#" + multibox).val().split(" ").filter(function (elt) {
  return elt.trim() != "";
}).length;
0 Kudos
Reply
AussieBattler10
Canvas Initiate

Re: SP 2013 - Limit a ML TextBox entry to 250 words

Hi @Tarf 

 

Thanks for that. I still have a few questions.

 

- in your code at which point am I referring to my JS variable 'projsumm'. I assume its 'Multibox'

- also at which point am I defining the word limit amount of 250? I don't see any mention of a number in your code to define this point.

 

Thanks again for your help

 

Cheers,

0 Kudos
Reply
Tarf
Design Canvas Artiste

Re: SP 2013 - Limit a ML TextBox entry to 250 words

Hi @AussieBattler10,

sorry I was totally missing on the validation part.

Go to the custom js section of your form and paste this code (change the variables to match yours):

function validateMulti(){
var len = NWF$("#" + multibox).val().split(" ").filter(function (elt) {   return elt.trim() != ""; }).length;
console.log(len)
return len > 250;
}

Add a validation rule to the multi-line text.
Control is invalid if... -> validateMulti()

And thats it.

I have a console.log in the validation function to see if it is called. You can of course remove this.

0 Kudos
Reply