Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Counting words in text box CS

Posted on 2014-03-01
5
Medium Priority
?
437 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 2000 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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

670 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