Solved

Counting words in text box CS

Posted on 2014-03-01
5
433 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 53

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 53

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
Suggested Courses

734 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