function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Abhishek Kumar 407Abhishek Kumar 407 

Can someone help here as I've a requirement: How to count no. of words in a given email body? I am unable to achieve it!

Here is the apex and VF code:
//Apex Code

 public class sendEmailbuttonApex
{
    public Account accounts            {set;get;}
    public String subject { get; set; }
    public String body { get; set; }
    public sendEmailbuttonApex(Apexpages.StandardController controller)
    {
        accounts=(Account)controller.getRecord();
    }
    public PageReference sendEmail()
    {
        List<Contact> conList=[Select Id, Email from contact where Email='abhishekk.twopirconsulting@gmail.com'];
        system.debug(conList);
        List<String> mail=new List<String>();
        for(Contact c:conList)
        {
            mail.add(c.Email);
        }
        Messaging.SingleEmailMessage msg1=new Messaging.SingleEmailMessage();
        msg1.setToAddresses(mail);
        msg1.setSubject(subject);
        msg1.setPlainTextBody(body);
        Messaging.Email[] emails=new Messaging.Email[]{msg1};
        Messaging.sendEmail(emails);
        PageReference p=new PageReference('/'+accounts.id);
        p.setRedirect(true);
        return p;

    }
}
 
//VF code

<apex:page docType="HTML-5.0" standardController="Account" extensions="sendEmailbuttonApex">
<apex:form>
      <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
        <script>
        $(document).ready(function() {
            $("#word_count").on('keydown', function(e) {
            //j$('[id$=word_count]').on('keydown', function(e) {
        var words = $.trim(this.value).length ? this.value.match(/\S+/g).length : 0;
        if (words <= 200) {
            $('#display_count').text(words);
            $('#word_left').text(200-words)
        }else{
            if (e.which !== 8) e.preventDefault();
        }
    });
 }); 
       </script>
   
    
		<br /><br />
			<apex:outputLabel value="Subject" for="Subject"/>:<br />     
			<apex:inputText value="{!subject}" id="Subject" maxlength="80"/>
			<br /><br />
			<apex:outputLabel value="Body" for="word_count"/>:<br />     
             <apex:inputTextarea id="word_count" cols="30" rows="10" value="{!body}"/>
        <br />
Total word Count : <span id="display_count">0</span> words. Words left : <span id="word_left">200</span>

			<br /><br /><br />
        <apex:commandButton value="Send Email" action="{!sendEmail}" /> 
		</apex:form>
</apex:page>

 
Best Answer chosen by Abhishek Kumar 407
Raj VakatiRaj Vakati
Use this code .  VisualForce Element Ids in jQuery selectors​ was not correct 
 
<apex:page docType="HTML-5.0" standardController="Account" extensions="sendEmailbuttonApex">
    <head lang="en">
        <meta charset="utf-8"/>
        <meta name="viewport" content="width=device-width,initial-scale=1.0"/>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
        
        </head>
        <body>
            
            <apex:form>
                <script src="https://code.jquery.com/jquery-1.8.3.min.js"></script>
       
        
        
        <br /><br />
        <apex:outputLabel value="Subject" for="Subject"/>:<br />     
        <apex:inputText value="{!subject}" id="Subject" maxlength="80"/>
        <br /><br />
        <apex:outputLabel value="Body" for="word_count"/>:<br />     
        <apex:inputTextarea id="word_count" cols="30" rows="10" value="{!body}" />
        <br />
        Total word Count : <span id="display_count">0</span> words. Words left : <span id="word_left">200</span>
        
        <br /><br /><br />
        <apex:commandButton value="Send Email" action="{!sendEmail}" /> 
    </apex:form>
    
     <script>
 
        $(document).ready(function() {

            //   alert('Hello, jQuery');
            
            $('[id$=word_count]').on('keyup', function(e) {
                //  alert('hey');
                //j$('[id$=word_count]').on('keydown', function(e) {
                var words = $.trim(this.value).length ? this.value.match(/\S+/g).length : 0;
                if (words <= 200) {
                   $('[id$=display_count]').text(words);
                   $('[id$=word_left]').text(200-words)
                }else{
                    if (e.which !== 8) e.preventDefault();
                }
            });
        }); 
        
       
        </script>
    
</body>


</apex:page>

 

All Answers

Raj VakatiRaj Vakati
Use this code .  VisualForce Element Ids in jQuery selectors​ was not correct 
 
<apex:page docType="HTML-5.0" standardController="Account" extensions="sendEmailbuttonApex">
    <head lang="en">
        <meta charset="utf-8"/>
        <meta name="viewport" content="width=device-width,initial-scale=1.0"/>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
        
        </head>
        <body>
            
            <apex:form>
                <script src="https://code.jquery.com/jquery-1.8.3.min.js"></script>
       
        
        
        <br /><br />
        <apex:outputLabel value="Subject" for="Subject"/>:<br />     
        <apex:inputText value="{!subject}" id="Subject" maxlength="80"/>
        <br /><br />
        <apex:outputLabel value="Body" for="word_count"/>:<br />     
        <apex:inputTextarea id="word_count" cols="30" rows="10" value="{!body}" />
        <br />
        Total word Count : <span id="display_count">0</span> words. Words left : <span id="word_left">200</span>
        
        <br /><br /><br />
        <apex:commandButton value="Send Email" action="{!sendEmail}" /> 
    </apex:form>
    
     <script>
 
        $(document).ready(function() {

            //   alert('Hello, jQuery');
            
            $('[id$=word_count]').on('keyup', function(e) {
                //  alert('hey');
                //j$('[id$=word_count]').on('keydown', function(e) {
                var words = $.trim(this.value).length ? this.value.match(/\S+/g).length : 0;
                if (words <= 200) {
                   $('[id$=display_count]').text(words);
                   $('[id$=word_left]').text(200-words)
                }else{
                    if (e.which !== 8) e.preventDefault();
                }
            });
        }); 
        
       
        </script>
    
</body>


</apex:page>

 
This was selected as the best answer
Abhishek Kumar 407Abhishek Kumar 407
Thanks a lot Raj v for fixing this problem as I am new to jQuery and Javascript. So no idea about it. Now the code started working.