Solved

Counting words in text box CS

Posted on 2014-03-01
5
430 Views
Last Modified: 2014-03-06
Dear experts,
I found the following code online to help me do a simple word count in a multi-line text box. (http://mrbool.com/how-to-count-char-word-and-line-in-a-textbox-using-asp-net-and-csharp/25288)

The code is pretty straight forward but I can't get it to display the count in the label "lblWordCount".

Sure would appreciate it if you could help me see what I'm missing.

Thanks

HTML:

<div style="text-align: center; color: #000088;">
    <span style="font-size: large; color: #000088">Tell us how God has worked in 
    your life!</span><br />
 <asp:TextBox TextMode="MultiLine" ID="txtCharInput" runat="server" Height="240px"
            Width="515px"></asp:TextBox> 
        <br />
        <br />
        Please limit your words to 250<br />
        
        <span style="color: #000088">Word Count:</span>
        <asp:Label ID="lblWordCount" runat="server" Text=""></asp:Label>
        <br />

</div>

Open in new window


CS

using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _hbiyg_FaithStories_AddYourStory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lblWordCount.Text = "0";
        }

    }
    private long WordCount(string strInput)
    {
        long intCount = 0;
        for (int i = 1; i < strInput.Length; i++)
        {
            if (char.IsWhiteSpace(strInput[i - 1]) == true)
            {
                if (char.IsLetterOrDigit(strInput[i]) == true ||
                    char.IsPunctuation(strInput[i]))
                {
                    intCount++;
                }
            }
        }
        if (strInput.Length > 2)
        {
            intCount++;
        }
        return intCount;
        lblWordCount.Text = WordCount(txtCharInput.Text).ToString();
    }

}

Open in new window

0
Comment
Question by:bobbellows
  • 2
  • 2
5 Comments
 
LVL 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 500 total points
ID: 39897995
Would you consider doing this client side? It seems to make more sense and will give instant feedback.  There are many js/jquery plug ins.  This is one http://qwertypants.github.io/jQuery-Word-and-Character-Counter-Plugin

I have a working sample here http://jsbin.com/gocis/1/edit?html,output and code below.

<!DOCTYPE html>
<html>
<head>
<link href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.min.css" rel="stylesheet" type="text/css" />
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.min.js"></script>
<script src="http://jsbin.com/rocih/1.js"></script>
  <script>
$(function() {
  $("#wordDown").counter({
    type: 'word', 
    goal: 20
});
});
    </script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
<textarea id="wordDown" cols="40" rows="5"></textarea>
</body>
</html>

Open in new window

0
 

Author Comment

by:bobbellows
ID: 39898476
Ok. I agree. I went to github and downloaded the plugin. How do I install it from the zip file? I'm new to this.
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39898484
I think this shows how to do that http:#a39897995

You will need to add jquery ui
<link href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.min.css" rel="stylesheet" type="text/css" />
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.min.js"></script>

Then you will add the js file
<script src="YourJSFolder/word-and-character-counter.min.js"></script>
and finally the jquery you want to use
$(function() {
  $("#wordDown").counter({
    type: 'word', 
    goal: 20
});
});

Open in new window

The other files
0
 

Author Comment

by:bobbellows
ID: 39909125
Sorry it took so long to get back to this. My day job had me working some overtime. It worked perfectly. Thanks.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now