Regular expression for word counting

Discussion in 'ASP .Net' started by drasko, Jul 14, 2007.

  1. drasko

    drasko Guest

    Hi, I have RegularExpressionValidator control and I wish to set it up
    for limiting input text in textarea to 50 words. Users can enter text
    they like, using all ".",",","!","?","@" and other symbols. All that
    matters is that there can be maximum 49 whitespace characters. How can
    write regular expression for this one?
     
    drasko, Jul 14, 2007
    #1
    1. Advertising

  2. On Jul 14, 11:57 am, drasko <> wrote:
    > Hi, I have RegularExpressionValidator control and I wish to set it up
    > for limiting input text in textarea to 50 words. Users can enter text
    > they like, using all ".",",","!","?","@" and other symbols. All that
    > matters is that there can be maximum 49 whitespace characters. How can
    > write regular expression for this one?


    Regex regex = new Regex(@"\w+");
    MatchCollection matches = regex.Matches(inputtext);

    if(matches.Count > 50)
    {
    // Error
    }
     
    Alexey Smirnov, Jul 14, 2007
    #2
    1. Advertising

  3. * drasko wrote, On 14-7-2007 11:57:
    > Hi, I have RegularExpressionValidator control and I wish to set it up
    > for limiting input text in textarea to 50 words. Users can enter text
    > they like, using all ".",",","!","?","@" and other symbols. All that
    > matters is that there can be maximum 49 whitespace characters. How can
    > write regular expression for this one?
    >


    This one comes close:

    ^(\S+\s){0,49}\S+$

    It does require the string not to begin with whitepaces, nor end with
    it. So you might want to put a javascript onBlur on the textbox to trim
    the contents.

    It reads as follows:

    ^ ensure we check the string from the beginning
    \S+ find a 'word' (something with no whitespaces)
    \s followed by a whitespace
    repeat this 0 up to 49 times

    followed by \S+ which is another 'word'.
    $ ensure we check the string all the way to the end.

    Jesse
     
    Jesse Houwing, Jul 14, 2007
    #3
  4. On Jul 14, 2:18 pm, Jesse Houwing <>
    wrote:
    > * drasko wrote, On 14-7-2007 11:57:
    >
    > > Hi, I have RegularExpressionValidator control and I wish to set it up
    > > for limiting input text in textarea to 50 words. Users can enter text
    > > they like, using all ".",",","!","?","@" and other symbols. All that
    > > matters is that there can be maximum 49 whitespace characters. How can
    > > write regular expression for this one?

    >
    > This one comes close:
    >
    > ^(\S+\s){0,49}\S+$
    >
    > It does require the string not to begin with whitepaces, nor end with
    > it. So you might want to put a javascript onBlur on the textbox to trim
    > the contents.
    >
    > It reads as follows:
    >
    > ^ ensure we check the string from the beginning
    > \S+ find a 'word' (something with no whitespaces)
    > \s followed by a whitespace
    > repeat this 0 up to 49 times
    >
    > followed by \S+ which is another 'word'.
    > $ ensure we check the string all the way to the end.
    >
    > Jesse


    Ah, Jesse you're right, I forgot about RegularExpressionValidator...
     
    Alexey Smirnov, Jul 14, 2007
    #4
  5. drasko

    drasko Guest

    Guys, thank you very much. Does your reg ex include , . ! ? or similar
    at the end, or somewhere in the middle?
     
    drasko, Jul 14, 2007
    #5
  6. drasko

    drasko Guest

    Guys, thank you so much!!!
     
    drasko, Jul 14, 2007
    #6
  7. drasko

    drasko Guest

    Doesn't work... :(
     
    drasko, Jul 14, 2007
    #7
  8. On Jul 14, 10:19 pm, drasko <> wrote:
    > Doesn't work... :(


    Use this one

    <asp:TextBox ID="TextBox1" runat="server" Height="184px"
    TextMode="MultiLine" Width="224px"></asp:TextBox>

    <asp:RegularExpressionValidator
    ID="RegularExpressionValidator1" runat="server"
    ControlToValidate="TextBox1"
    ErrorMessage="RegularExpressionValidator" ValidationExpression="(((^
    \s*)*\S+\s+)|(\S+)){1,50}"
    Display="Dynamic"></asp:RegularExpressionValidator>

    <asp:Button ID="Button1" runat="server" CausesValidation="true"
    OnClick="Button1_Click1" Text="Button" />

    This is used to validate a word count, not a whitespace characters.
    So, if you need to be sure that there is no space at the end, make a
    trim at the code-behind...
     
    Alexey Smirnov, Jul 14, 2007
    #8
  9. * drasko wrote, On 14-7-2007 22:19:
    > Doesn't work... :(
    >


    The regex I sent does not allow for multiple whitespace characters right
    after eachother.

    Altering it to:

    ^(\S+\s+){0,49}\S+$

    should work, but that counts up to 49 gaps, not up to 49 exact
    whitespace characters.

    If you want to be more exact I suggest you sue a custom validator and
    write the javascript and server side logic yourself using the normal
    String functions.

    Jesse
     
    Jesse Houwing, Jul 15, 2007
    #9
  10. * drasko wrote, On 14-7-2007 22:19:
    > Doesn't work... :(
    >


    Can you provide us with the texts you've tried? That way I can verify
    what's going wrong.

    Jesse
     
    Jesse Houwing, Jul 15, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,355
  2. Replies:
    4
    Views:
    1,143
    Paul D
    Aug 26, 2006
  3. Summercool
    Replies:
    13
    Views:
    716
    Dr.Ruud
    Feb 1, 2008
  4. Summercool
    Replies:
    22
    Views:
    432
    Ryan Holmes
    Aug 6, 2010
  5. edwardfredriks

    counting up instead of counting down

    edwardfredriks, Sep 6, 2005, in forum: Javascript
    Replies:
    6
    Views:
    225
    Dr John Stockton
    Sep 7, 2005
Loading...

Share This Page