0

I am trying to limit the length of the "Project Notes" Custom Field in Project Details Page (PDP) using Project Server 2010. I am using the following jQuery in a Content Editor Web Part added to the PDP:

<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
<script>
    $(document).ready(function(){ 
        var textArea = $("textarea[title$='Project Notes']");
        textArea.keypress(function() 
        { 
            var text = textArea.text();
            if(text.length > 10) 
            { 
                alert("Project Notes cannot exceed 100 characters in length."); 
                textArea.val(text.substring(0,9)); 
            } 
        }); 
    });
</script>

However, when the user types into the text area nothing is happening.The event does not fire. I have tried various modifications to the code but the same result. Please let me know what I am missing.

Thank you.

Moe
  • 13
  • 3

2 Answers2

0

You can use maxlength attribute for html element.

<textarea maxlength="10" rows='5' cols='25'></textarea>

and if we go with your script then use .val() instead of .text(). form elements does have values where you have to input the text. whether that is input type text or textarea.

var textArea = $("textarea[title$='Project Notes']");
textArea.keypress(function () {
   var text = textArea.val();
   if (text.length > 10) {
      alert("Project Notes cannot exceed 100 characters in length.");
      textArea.val(text.substring(0, 9));
   }
});

You can try this out in fiddle here

Jai
  • 74,255
  • 12
  • 74
  • 103
  • Thank you for your comments, however, the suggested solution does not work. The problem is that the event is not firing inside the Project Server page. Even if I remove the the code inside the keypress function and keep a simple alert, it still does not execute as well. I tried using keyup instead of keypress but same result. Events are not firing when the user types into the text area. – Moe Mar 04 '13 at 05:39
0

I have changed the text area to a text box and the events started firing normally.

Moe
  • 13
  • 3