Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 441
  • Last Modified:

Counting words in text box CS

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
bobbellows
Asked:
bobbellows
  • 2
  • 2
1 Solution
 
Scott Fell, EE MVEDeveloperCommented:
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
 
bobbellowsAuthor Commented:
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
 
Scott Fell, EE MVEDeveloperCommented:
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
 
bobbellowsAuthor Commented:
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 to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now