473,837 Members | 1,450 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:RequiredFi eldValidator
ID="RequiredFie ldValidator1"
runat="server"
ErrorMessage="R equiredFieldVal idator"
ControlToValida te="TextBox1"> </asp:RequiredFie ldValidator>
<asp:RegularExp ressionValidato r
ID="RegularExpr essionValidator 1"
runat="server"
ErrorMessage="R egularExpressio nValidator"
ControlToValida te="TextBox1"
ValidationExpre ssion="[a-z]"></asp:RegularExpr essionValidator >
<asp:TextBox ID="TextBox2" runat="server"> </asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClick="Button 1_Click" />

protected void Button1_Click(o bject sender, EventArgs e)
{
if (Page.IsValid)
{
Response.Write( "Page is valid");
}
else
{
Response.Write( "Page is not valid");
}
}

Oct 3 '07 #1
6 7990
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 RequiredFieldVa lidator.

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.comwro te:
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:RequiredFi eldValidator
ID="RequiredFie ldValidator1"
runat="server"
ErrorMessage="R equiredFieldVal idator"
ControlToValida te="TextBox1"> </asp:RequiredFie ldValidator>
<asp:RegularExp ressionValidato r
ID="RegularExpr essionValidator 1"
runat="server"
ErrorMessage="R egularExpressio nValidator"
ControlToValida te="TextBox1"
ValidationExpre ssion="[a-z]"></asp:RegularExpr essionValidator >
<asp:TextBox ID="TextBox2" runat="server"> </asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClick="Button 1_Click" />

protected void Button1_Click(o bject 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:RequiredFi eldValidator
ID="RequiredFie ldValidator1"
runat="server"
ErrorMessage="R equiredFieldVal idator"
ControlToValida te="TextBox1"> </asp:RequiredFie ldValidator>
<asp:RegularExp ressionValidato r
ID="RegularExpr essionValidator 1"
runat="server"
ErrorMessage="R egularExpressio nValidator"
ControlToValida te="TextBox1"
ValidationExpre ssion="[a-z]"></asp:RegularExpr essionValidator >
<asp:TextBox ID="TextBox2" runat="server"> </asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClick="Button 1_Click" />
protected void Button1_Click(o bject 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 RequiredFieldVa lidator.
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.comwro te:
>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:RequiredF ieldValidator
ID="RequiredFi eldValidator1"
runat="serve r"
ErrorMessage=" RequiredFieldVa lidator"
ControlToValid ate="TextBox1"> </asp:RequiredFie ldValidator>
<asp:RegularEx pressionValidat or
ID="RegularExp ressionValidato r1"
runat="serve r"
ErrorMessage=" RegularExpressi onValidator"
ControlToValid ate="TextBox1"
ValidationExpr ession="[a-z]"></asp:RegularExpr essionValidator >
<asp:TextBox ID="TextBox2" runat="server"> </asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClick="Butto n1_Click" />
protected void Button1_Click(o bject 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:RequiredF ieldValidator
ID="RequiredFi eldValidator1"
runat="serve r"
ErrorMessage=" RequiredFieldVa lidator"
ControlToValid ate="TextBox1"> </asp:RequiredFie ldValidator>
<asp:RegularEx pressionValidat or
ID="RegularExp ressionValidato r1"
runat="serve r"
ErrorMessage=" RegularExpressi onValidator"
ControlToValid ate="TextBox1"
ValidationExpr ession="[a-z]"></asp:RegularExpr essionValidator >
<asp:TextBox ID="TextBox2" runat="server"> </asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClick="Butto n1_Click" />
protected void Button1_Click(o bject 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:TextBo x ID="TextBox1" runat="server"> </asp:TextBox>
<asp:Required FieldValidator
ID="RequiredF ieldValidator1"
runat="server "
ErrorMessage= "RequiredFieldV alidator"
ControlToVali date="TextBox1" ></asp:RequiredFie ldValidator>
<asp:RegularE xpressionValida tor
ID="RegularEx pressionValidat or1"
runat="server "
ErrorMessage= "RegularExpress ionValidator"
ControlToVali date="TextBox1"
ValidationExp ression="[a-z]"></asp:RegularExpr essionValidator >
<asp:TextBo x ID="TextBox2" runat="server"> </asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClick="Butt on1_Click" />
protected void Button1_Click(o bject sender, EventArgs e)
{
if (Page.IsValid)
{
Response.Writ e("Page is valid");
}
else
{
Response.Writ e("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.comwro te:
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:RequiredFi eldValidator
ID="RequiredFie ldValidator1"
runat="server"
ErrorMessage="R equiredFieldVal idator"
ControlToValida te="TextBox1"> </asp:RequiredFie ldValidator>
<asp:RegularExp ressionValidato r
ID="RegularExpr essionValidator 1"
runat="server"
ErrorMessage="R egularExpressio nValidator"
ControlToValida te="TextBox1"
ValidationExpre ssion="[a-z]"></asp:RegularExpr essionValidator >
<asp:TextBox ID="TextBox2" runat="server"> </asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClick="Button 1_Click" />

protected void Button1_Click(o bject 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
18086
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 want to happen is the Regular Expression Validator to return false only when the string contains PO Box. Currently it is false even when a valid address exists.
3
2225
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 a hard return, it bombs regardless of length. How do you allow hard returns in the following regular expression? Thanks in advance! ^.{0,25}$ Mark
2
2949
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 Expression of the validator based on the
2
2258
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
5583
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 expression is username between 3 and 20 chars When the user enters characters other than the specified REVs are working. But if the user leaves the textbox without entering anything Page.IsValid is true which indicates it is not performng any...
2
9877
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, such as change the border color of the textbox to red? That would look quite nice: if the expression fails, the red validator text is shown, plus the textbox's border goes red. TIA,
5
4474
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 expression validator expecting a message to be displayed but it let me submit the form. Do I have to use both?
2
1399
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 the user to have to select a name other than 'Please Select'. I am thinking maybe the Regular Expression Validator is the best move
1
2271
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 jpg. I have the following expression which validates fine in a .net tester I use and also a javascript tester. But when I use the following path on the page, it gives me the error message that I haven't entered a valid image. ...
2
1836
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. Everything I have read thus far involves checking only for the presence or absence of characters. Can anyone provide an RE that will correctly evaluate a string to see if it contains other strings, specifically these (omit the quotes)..."<script>",...
0
9682
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10566
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10271
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9398
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7806
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7000
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5668
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5848
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3124
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.