Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Regular Expression Validator

Reply
Thread Tools

Regular Expression Validator

 
 
David
Guest
Posts: n/a
 
      10-03-2007
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");
}
}

 
Reply With Quote
 
 
 
 
Mike Placentra II
Guest
Posts: n/a
 
      10-03-2007
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 <(E-Mail Removed)> 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");
> }
> }


 
Reply With Quote
 
 
 
 
Jesse Houwing
Guest
Posts: n/a
 
      10-03-2007
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


 
Reply With Quote
 
Jesse Houwing
Guest
Posts: n/a
 
      10-03-2007
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 <(E-Mail Removed)> 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


 
Reply With Quote
 
Hans Kesting
Guest
Posts: n/a
 
      10-04-2007
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


 
Reply With Quote
 
Jesse Houwing
Guest
Posts: n/a
 
      10-04-2007
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


 
Reply With Quote
 
David
Guest
Posts: n/a
 
      10-04-2007
Thanks to everyone for the help, I have got this working now.

On Oct 3, 11:57 am, David <(E-Mail Removed)> 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");
> }
> }



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expression Validator seems to be wrong! Edward ASP .Net 0 12-04-2003 05:31 PM
P.O. Box Regular Expression Validator - heeeeeelp? Simple Simon ASP .Net 2 11-04-2003 05:10 PM
Regular Expression Validator Bryce Budd ASP .Net 1 09-18-2003 05:51 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM
Re: howto use c# regular expression validator to exclude file types Brian Vallelunga ASP .Net 2 07-31-2003 10:27 PM



Advertisments