473,385 Members | 1,453 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Regular Expression Validator

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
6 7907
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bryce Budd | last post by:
Hi all, I am trying to use a regular expression validator to check for the existence of PO Box in an address textbox. The business rule is "No addresses with PO Boxes are allowed." What I...
3
by: Mark | last post by:
To validate the length of a multiline textbox, I'm told that I have to use a regular expression validator. The regular expression below limits it to 25 characters in length, but if the user enters...
2
by: VSK | last post by:
Hi all, I have a .ascx file with dropdownbox (SSN, EmpName) textbox submit button regular expression validator( controltovalidate is the above textbox) Now i want to change the Regular...
2
by: Nazir | last post by:
Hi I'm using a regular expression validator, but if spaces are entered, it bypasses the validation! I'm using ^{5,100}$
2
by: S.Kartikeyan | last post by:
I have the following problem. I am using the follwing Regular Expression validator(REV) with validator expressions ^{1,2}$ ^{3,20}$ The idea of the first exp is 1 or 2 digits the idea of second...
2
by: Dot net work | last post by:
Hello. Say I have a .net textbox that uses a .net regularexpressionvalidator. If the regular expression fails, is it possible to launch a small client side javascript function to do something,...
5
by: John . | last post by:
I am using the Regular Expression Validator control to validate a correct email address. But, at the same time I would like to make it a required field. I tested by using just the regular...
2
by: kieran | last post by:
Hi, I am using Visual Studio 2005 and am trying to use a Regular Expression Validator control. I have a drop down list which contains various names, the first one is "Please Select". I want...
1
by: vtxr1300 | last post by:
I'm having a problem with a regular expression in conjunction with the regular expression validator. I am trying to make sure that when a user browses for a file to upload, it ends in gif, jpeg or...
2
by: Joey | last post by:
Hello guys, I'm trying to learn about regular expressions. I need to be able to use an RE that can evaluate for STRINGS (or specific sequences of characters), not just occurances of characters....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.