Regular Expression Validator

Discussion in 'ASP .Net' started by David, Oct 3, 2007.

  1. David

    David Guest

    I'm having trouble getting the regular expression validator to work
    with a text box.

    In this simple example I only want lower case letters to be allowed.
    So I tried the following and it doesn't work, would somebody be so
    kind as to tell me why.

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator
    ID="RequiredFieldValidator1"
    runat="server"
    ErrorMessage="RequiredFieldValidator"
    ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
    <asp:RegularExpressionValidator
    ID="RegularExpressionValidator1"
    runat="server"
    ErrorMessage="RegularExpressionValidator"
    ControlToValidate="TextBox1"
    ValidationExpression="[a-z]"></asp:RegularExpressionValidator>
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button"
    OnClick="Button1_Click" />

    protected void Button1_Click(object sender, EventArgs e)
    {
    if (Page.IsValid)
    {
    Response.Write("Page is valid");
    }
    else
    {
    Response.Write("Page is not valid");
    }
    }
     
    David, Oct 3, 2007
    #1
    1. Advertising

  2. Try "^[a-z]+$". That will also take care of requiring some text to be
    entered, so if you're OK with having one error message taking care of
    "you must enter stuff" and also "you must enter lowercase letters
    only", IE "You must enter lowercase English letters", you can delete
    your RequiredFieldValidator.

    The character group square brackets mean to match one character from
    that group, so your current Regular Expression means the user must
    enter one lowercase letter, possibly among other things. The +
    modifier means it must match any of those characters at least once,
    and you could also use the * modifier to match any of those characters
    any number of times (zero is OK). Putting ^ at the beginning means it
    must match that starting with the beginning, and ending with $ means
    it must match that at the end, so together they mean the entire string
    must match it.

    -Michael Placentra II


    On Oct 3, 12:57 pm, David <> wrote:
    > I'm having trouble getting the regular expression validator to work
    > with a text box.
    >
    > In this simple example I only want lower case letters to be allowed.
    > So I tried the following and it doesn't work, would somebody be so
    > kind as to tell me why.
    >
    > <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    > <asp:RequiredFieldValidator
    > ID="RequiredFieldValidator1"
    > runat="server"
    > ErrorMessage="RequiredFieldValidator"
    > ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
    > <asp:RegularExpressionValidator
    > ID="RegularExpressionValidator1"
    > runat="server"
    > ErrorMessage="RegularExpressionValidator"
    > ControlToValidate="TextBox1"
    > ValidationExpression="[a-z]"></asp:RegularExpressionValidator>
    > <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    > <asp:Button ID="Button1" runat="server" Text="Button"
    > OnClick="Button1_Click" />
    >
    > protected void Button1_Click(object sender, EventArgs e)
    > {
    > if (Page.IsValid)
    > {
    > Response.Write("Page is valid");
    > }
    > else
    > {
    > Response.Write("Page is not valid");
    > }
    > }
     
    Mike Placentra II, Oct 3, 2007
    #2
    1. Advertising

  3. Hello David,

    > I'm having trouble getting the regular expression validator to work
    > with a text box.
    >
    > In this simple example I only want lower case letters to be allowed.
    > So I tried the following and it doesn't work, would somebody be so
    > kind as to tell me why.
    >
    > <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    > <asp:RequiredFieldValidator
    > ID="RequiredFieldValidator1"
    > runat="server"
    > ErrorMessage="RequiredFieldValidator"
    > ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
    > <asp:RegularExpressionValidator
    > ID="RegularExpressionValidator1"
    > runat="server"
    > ErrorMessage="RegularExpressionValidator"
    > ControlToValidate="TextBox1"
    > ValidationExpression="[a-z]"></asp:RegularExpressionValidator>
    > <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    > <asp:Button ID="Button1" runat="server" Text="Button"
    > OnClick="Button1_Click" />
    > protected void Button1_Click(object sender, EventArgs e)
    > {
    > if (Page.IsValid)
    > {
    > Response.Write("Page is valid");
    > }
    > else
    > {
    > Response.Write("Page is not valid");
    > }
    > }


    Your expression allows only one lowercase character at the moment. [a-z]
    means one position in the range between a-z. Though I've been using regex
    quite extensively, I'm unsire if the RegexValidator is case insensitive by
    default (funny).

    The expression first should be: ^[a-z]+$

    ^ begin of input
    [a-z]+ one or more character in the range of a-z
    $ end of the string

    Should the regex validator be case insensitive by default, you can force
    the regex to case sensitive as follows:

    (?-i)^[a-z]+$

    or

    ^(?-i:[a-z]+)$

    the ?-i option switches of the case insensitive flag. Putting at the left
    side of the - would switch it on for that part of the regex.

    --
    Jesse Houwing
    jesse.houwing at sogeti.nl
     
    Jesse Houwing, Oct 4, 2007
    #3
  4. Hello Mike,

    > Try "^[a-z]+$". That will also take care of requiring some text to be
    > entered, so if you're OK with having one error message taking care of
    > "you must enter stuff" and also "you must enter lowercase letters
    > only", IE "You must enter lowercase English letters", you can delete
    > your RequiredFieldValidator.


    That isn't exactly true. The regexValidator by default does not trigger when
    the input is empty. This is done because the error message for a required
    field is different from an incorrect input.

    > The character group square brackets mean to match one character from
    > that group, so your current Regular Expression means the user must
    > enter one lowercase letter, possibly among other things. The +
    > modifier means it must match any of those characters at least once,
    > and you could also use the * modifier to match any of those characters
    > any number of times (zero is OK). Putting ^ at the beginning means it
    > must match that starting with the beginning, and ending with $ means
    > it must match that at the end, so together they mean the entire string
    > must match it.
    >
    > -Michael Placentra II
    >
    > On Oct 3, 12:57 pm, David <> wrote:
    >
    >> I'm having trouble getting the regular expression validator to work
    >> with a text box.
    >>
    >> In this simple example I only want lower case letters to be allowed.
    >> So I tried the following and it doesn't work, would somebody be so
    >> kind as to tell me why.
    >>
    >> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    >> <asp:RequiredFieldValidator
    >> ID="RequiredFieldValidator1"
    >> runat="server"
    >> ErrorMessage="RequiredFieldValidator"
    >> ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
    >> <asp:RegularExpressionValidator
    >> ID="RegularExpressionValidator1"
    >> runat="server"
    >> ErrorMessage="RegularExpressionValidator"
    >> ControlToValidate="TextBox1"
    >> ValidationExpression="[a-z]"></asp:RegularExpressionValidator>
    >> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    >> <asp:Button ID="Button1" runat="server" Text="Button"
    >> OnClick="Button1_Click" />
    >> protected void Button1_Click(object sender, EventArgs e)
    >> {
    >> if (Page.IsValid)
    >> {
    >> Response.Write("Page is valid");
    >> }
    >> else
    >> {
    >> Response.Write("Page is not valid");
    >> }
    >> }

    --
    Jesse Houwing
    jesse.houwing at sogeti.nl
     
    Jesse Houwing, Oct 4, 2007
    #4
  5. David

    Hans Kesting Guest

    Jesse Houwing was thinking very hard :
    > Hello David,
    >
    >> I'm having trouble getting the regular expression validator to work
    >> with a text box.
    >>
    >> In this simple example I only want lower case letters to be allowed.
    >> So I tried the following and it doesn't work, would somebody be so
    >> kind as to tell me why.
    >>
    >> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    >> <asp:RequiredFieldValidator
    >> ID="RequiredFieldValidator1"
    >> runat="server"
    >> ErrorMessage="RequiredFieldValidator"
    >> ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
    >> <asp:RegularExpressionValidator
    >> ID="RegularExpressionValidator1"
    >> runat="server"
    >> ErrorMessage="RegularExpressionValidator"
    >> ControlToValidate="TextBox1"
    >> ValidationExpression="[a-z]"></asp:RegularExpressionValidator>
    >> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    >> <asp:Button ID="Button1" runat="server" Text="Button"
    >> OnClick="Button1_Click" />
    >> protected void Button1_Click(object sender, EventArgs e)
    >> {
    >> if (Page.IsValid)
    >> {
    >> Response.Write("Page is valid");
    >> }
    >> else
    >> {
    >> Response.Write("Page is not valid");
    >> }
    >> }

    >
    > Your expression allows only one lowercase character at the moment. [a-z]
    > means one position in the range between a-z. Though I've been using regex
    > quite extensively, I'm unsire if the RegexValidator is case insensitive by
    > default (funny).
    >
    > The expression first should be: ^[a-z]+$
    >
    > ^ begin of input
    > [a-z]+ one or more character in the range of a-z
    > $ end of the string
    >
    > Should the regex validator be case insensitive by default, you can force the
    > regex to case sensitive as follows:
    >
    > (?-i)^[a-z]+$
    >
    > or
    >
    > ^(?-i:[a-z]+)$
    >
    > the ?-i option switches of the case insensitive flag. Putting at the left
    > side of the - would switch it on for that part of the regex.


    The javascript that will be evaluating this regexp doesn't know about
    advanced concepts like (?-i). Also the validator makes sure that the
    regexp matches the entire string: it behaves as if the expression is
    surrounded by ^ and $.

    Hans Kesting
     
    Hans Kesting, Oct 4, 2007
    #5
  6. Hello Hans,

    > Jesse Houwing was thinking very hard :
    >
    >> Hello David,
    >>
    >>> I'm having trouble getting the regular expression validator to work
    >>> with a text box.
    >>>
    >>> In this simple example I only want lower case letters to be allowed.
    >>> So I tried the following and it doesn't work, would somebody be so
    >>> kind as to tell me why.
    >>>
    >>> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    >>> <asp:RequiredFieldValidator
    >>> ID="RequiredFieldValidator1"
    >>> runat="server"
    >>> ErrorMessage="RequiredFieldValidator"
    >>> ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
    >>> <asp:RegularExpressionValidator
    >>> ID="RegularExpressionValidator1"
    >>> runat="server"
    >>> ErrorMessage="RegularExpressionValidator"
    >>> ControlToValidate="TextBox1"
    >>> ValidationExpression="[a-z]"></asp:RegularExpressionValidator>
    >>> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    >>> <asp:Button ID="Button1" runat="server" Text="Button"
    >>> OnClick="Button1_Click" />
    >>> protected void Button1_Click(object sender, EventArgs e)
    >>> {
    >>> if (Page.IsValid)
    >>> {
    >>> Response.Write("Page is valid");
    >>> }
    >>> else
    >>> {
    >>> Response.Write("Page is not valid");
    >>> }
    >>> }

    >> Your expression allows only one lowercase character at the moment.
    >> [a-z] means one position in the range between a-z. Though I've been
    >> using regex quite extensively, I'm unsire if the RegexValidator is
    >> case insensitive by default (funny).
    >>
    >> The expression first should be: ^[a-z]+$
    >>
    >> ^ begin of input
    >> [a-z]+ one or more character in the range of a-z
    >> $ end of the string
    >> Should the regex validator be case insensitive by default, you can
    >> force the regex to case sensitive as follows:
    >>
    >> (?-i)^[a-z]+$
    >>
    >> or
    >>
    >> ^(?-i:[a-z]+)$
    >>
    >> the ?-i option switches of the case insensitive flag. Putting at the
    >> left side of the - would switch it on for that part of the regex.
    >>

    > The javascript that will be evaluating this regexp doesn't know about
    > advanced concepts like (?-i). Also the validator makes sure that the
    > regexp matches the entire string: it behaves as if the expression is
    > surrounded by ^ and $.


    Hans,

    You are completely correct. I remembered that Javascript supports 2 modifiers,
    but that these could only be applied to a complete regex had slipped my mind.


    As for the validator automatically applying a ^ and a $, yes that is correct,
    but for clarity and re-use (the whole expression in a Regex Object server-side)
    I always supply the ^..$ myself.

    Som research leasds me to conclude that a RegexValidator is case sensitive
    by default. And as the /i modifier cannot be supplied a different expression
    is required:

    ^[A-Za-z]+$

    --
    Jesse Houwing
    jesse.houwing at sogeti.nl
     
    Jesse Houwing, Oct 4, 2007
    #6
  7. David

    David Guest

    Thanks to everyone for the help, I have got this working now.

    On Oct 3, 11:57 am, David <> wrote:
    > I'm having trouble getting the regular expression validator to work
    > with a text box.
    >
    > In this simple example I only want lower case letters to be allowed.
    > So I tried the following and it doesn't work, would somebody be so
    > kind as to tell me why.
    >
    > <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    > <asp:RequiredFieldValidator
    > ID="RequiredFieldValidator1"
    > runat="server"
    > ErrorMessage="RequiredFieldValidator"
    > ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
    > <asp:RegularExpressionValidator
    > ID="RegularExpressionValidator1"
    > runat="server"
    > ErrorMessage="RegularExpressionValidator"
    > ControlToValidate="TextBox1"
    > ValidationExpression="[a-z]"></asp:RegularExpressionValidator>
    > <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    > <asp:Button ID="Button1" runat="server" Text="Button"
    > OnClick="Button1_Click" />
    >
    > protected void Button1_Click(object sender, EventArgs e)
    > {
    > if (Page.IsValid)
    > {
    > Response.Write("Page is valid");
    > }
    > else
    > {
    > Response.Write("Page is not valid");
    > }
    > }
     
    David, Oct 4, 2007
    #7
    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. Brian Vallelunga
    Replies:
    2
    Views:
    8,357
    Brian Vallelunga
    Jul 31, 2003
  2. VSK
    Replies:
    2
    Views:
    2,387
  3. Bryce Budd

    Regular Expression Validator

    Bryce Budd, Sep 18, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    591
    Brian Davis
    Sep 18, 2003
  4. Simple Simon
    Replies:
    2
    Views:
    4,387
    Simple Simon
    Nov 4, 2003
  5. Edward
    Replies:
    0
    Views:
    848
    Edward
    Dec 4, 2003
Loading...

Share This Page