cancel
Showing results for 
Search instead for 
Did you mean: 

How to count the characters in textarea when typing? How to achieve in SAP CRM Web ui?

former_member226280
Discoverer
0 Kudos

Hello Experts,

I have one requirement, Need to count the characters in the textarea field when typing.

strlen( ), this string operation only count the total characters. But My requirement need the count the characters when typing.

Kindly Help me with solution and Thanks in advance.

Accepted Solutions (1)

Accepted Solutions (1)

former_member210661
Active Contributor
0 Kudos

Hi Jagannadh,

i will give one suggestion create 2 text area fields using  thtmlb:textArea tag here one is for note and another is for count...

ex:

it looks like this..

when ever your are changing the data in text box the count will vary..

this is the sample code..

<script type="text/javascript">

var Count = null;

function OnClick(onKeyUpTextArea) {

   if (Count == null) {

     thtmlbUtil.addEventHandler(onKeyUpTextArea, "keyup", myOnKeyUpHandler);

     Count = onKeyUpTextArea;

   }

}

function myOnKeyUpHandler() {

   if (Count) {

     var TextAreaID = Count.id.replace("onKeyUp", "reporting");

     var reportingTextArea   = document.getElementById(TextAreaID);

     if (reportingTextArea) {

       reportingTextArea.value = Count.value.length;

     }

   }

}

</script>

   <htmlb:label for  = "Note"

                text = "Type your data in Textarea/Textbox" />

<br />

<thtmlb:textArea id      = "onKeyUpTextArea"

                  text    = "Hello Experts..."

                  onClick = "OnClick(this);" />

   <htmlb:label for  = "Char"

                text = "Character Left" />               

<thtmlb:textArea id   = "reportingTextArea"

                  rows = "1"

                  text="16"/>

<br />

Try this and let me know if you have any queries..

Thanks & Regards,

Srinivas.

former_member226280
Discoverer
0 Kudos

Hi Srinivas,

Thanks for your quick response and helpful solution. But I need this requirement for standard text area field.  Is it working for the standard text area field. If its works for the standard text area field, then in the above solution where I will give the standard text area field name. I tried but not success.

Can you please explain elaborately ?

I am using this component : GSTEXT

                         view      : GSTEXT/Text     (Text.htm)

        text area filed name :  LINES.

Kindly refer the GSTEXT component. you will understand my requirement.

Former Member
0 Kudos

Hi Srinivas,

I am also having similar requirement. In the main text area, when I type more than 150 chars, it should raise warring message. I tried to use your above java scripting reference code with only one text area(standard Text field in Notes assignment block) with field name LINES.

Code line:

reportingTextArea.value = Count.value.length;

if "reportingTextArea.value" is greater than 150 Chars, I am calling "alert" command for raising the message but it didn't work.

alert( 'Please enter less than 150 Chars' );

Could you please suggest.

Thanks in advance!!

Regards,

Shaik

former_member210661
Active Contributor
0 Kudos

Hi Shaik,

First of all Sorry for the late reply...

In that case you have to get the standard text field ID (LINES) based on that count the characters which you have passed in the notes field and based on length have to rise the alert..

this is the snippet code for that..

<%

   data: lv_fieldname type string,

           lv_contextnode type string,

           lv_id type string.

           lv_fieldname = 'links'.   " fieldname ( only small letters )

           lv_contextnode = 'contextnode'" context node name in which the field is present.


   CONCATENATE controller->component_id '_' lv_contextnode '_' lv_fieldname INTO lv_id.

<script type="text/javascript">

var NoteID = document.getElementById("<%= lv_id %>");

NoteID.addEventListener("keyup", myFunction);

function myFunction() {

var count ;

count = document.getElementById("<%= lv_id %>").value;

if(count.length == 10) {

alert("Please enter less than 10 Chars ");

}

// this is for display the count in input field...

document.getElementById("NoteId").value = count.length;

}

</script>

<thtmlb:textArea id   = "NoteId"

                  rows = "1"

                  text = "" />

Regards,

Srinivas.

Answers (0)