By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,353 Members | 1,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,353 IT Pros & Developers. It's quick & easy.

Regular Expression Validator

P: n/a
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");
}
}

Oct 3 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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 <david.mccollo...@gmail.comwrote:
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");
}
}
Oct 3 '07 #2

P: n/a
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
Oct 3 '07 #3

P: n/a
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 <david.mccollo...@gmail.comwrote:
>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
Oct 3 '07 #4

P: n/a
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
Oct 4 '07 #5

P: n/a
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
Oct 4 '07 #6

P: n/a
Thanks to everyone for the help, I have got this working now.

On Oct 3, 11:57 am, David <david.mccollo...@gmail.comwrote:
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");
}
}

Oct 4 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.