Regular Expression Validator

D

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");
}
}
 
M

Mike Placentra II

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
 
J

Jesse Houwing

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.
 
J

Jesse Houwing

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

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");
}
}
 
H

Hans Kesting

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
 
J

Jesse Houwing

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]+$
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top