Solved

Counting words in text box CS

Posted on 2014-03-01
5
425 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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

8 Experts available now in Live!

Get 1:1 Help Now