473,670 Members | 2,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regular Expression Validator

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.

I need this validation to occur on the client, hence the
Regular Expression Validator control.

Here's the RE: I'm using in the ValidationExpre ssion
property:

[^(P\.?\s?O\.?\s Box)+]

This currently matches (returns false/invalid) for:
smith road and smith po box road?

Any insight into the proper regular expression to achieve
my goal would be greatly appreciated.
Thanks in advance,

Bryce
Jul 21 '05 #1
2 18070


Try this one out:

(?i)^((?<!P\.?\ s?O\.?\sBox).)+ (?<!P\.?\s?O\.? \sBox)$

The (?i) turns on the ignore case option and then the expression matches the
beginning of the string, followed by 1 or more characters that are not
preceded by P.O. Box, followed by the end of the string. The repeated
negative look-behind is there to make sure that a string containing only
"P.O. Box" is not matched.
Brian Davis
www.knowdotnet.com

"Bryce Budd" <bb***@fulltilt .com> wrote in message
news:0b******** *************** *****@phx.gbl.. .
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.

I need this validation to occur on the client, hence the
Regular Expression Validator control.

Here's the RE: I'm using in the ValidationExpre ssion
property:

[^(P\.?\s?O\.?\s Box)+]

This currently matches (returns false/invalid) for:
smith road and smith po box road?

Any insight into the proper regular expression to achieve
my goal would be greatly appreciated.
Thanks in advance,

Bryce

Jul 21 '05 #2
Do this:

go get a regex design/test tool, like
http://www.organicbit.com/regex/fog0000000019.html

Design and test the regex interactively using that tool.

When you think you have it, build a regex test app that tests all the
various combinations you can think of, and run it.
eg

namespace Ionic.Test.Emai lValidation {

/// <remarks>
/// Represents all the input for the test, including the regex to test,
/// and an array of test cases.
/// </remarks>
[System.Xml.Seri alization.XmlRo otAttribute("Em ail.Validation. Input",
Namespace="", IsNullable=fals e)]
public class TestInput {

/// <remarks/>

[System.Xml.Seri alization.XmlEl ementAttribute( Form=System.Xml .Schema.XmlSche
maForm.Unqualif ied)]
public string Regexp;

/// <remarks/>

[System.Xml.Seri alization.XmlAr rayAttribute(Fo rm=System.Xml.S chema.XmlSchema
Form.Unqualifie d)]
[System.Xml.Seri alization.XmlAr rayItemAttribut e("Case",
Form=System.Xml .Schema.XmlSche maForm.Unqualif ied, IsNullable=fals e)]
public TestCase[] TestList;
}
/// <remarks>
/// This is the type that stores a single test case.
/// We need a bunch of these to verify that the regex works as
/// expected. Each test case has an input and an output. In our
/// case, the input is a string, and the output is a bool value,
/// which indicates whether the Regex should match or not.
/// Other tests will have different input and output.
/// </remarks>
public class TestCase {

/// <remarks/>
[System.Xml.Seri alization.XmlAt tribute("Email" ,
Form=System.Xml .Schema.XmlSche maForm.Unqualif ied)]
public string Input;

/// <remarks/>
[System.Xml.Seri alization.XmlAt tribute("Valid" ,
Form=System.Xml .Schema.XmlSche maForm.Unqualif ied)]
public bool ExpectedOutput;
}
/// <remarks>
/// This is the test app. The main routine de-serializes from
/// an XML file, then runs the tests, comparing the expected
/// (or desired) output with the actual result.
/// </remarks>
public class Tester {

public static void Main() {
string InputPath= "EmailValidatio nInput.xml";

System.IO.FileS tream fs = new System.IO.FileS tream(InputPath ,
System.IO.FileM ode.Open);
System.Xml.Seri alization.XmlSe rializer s= new
System.Xml.Seri alization.XmlSe rializer(typeof (TestInput));
TestInput Input= (TestInput) s.Deserialize(f s);
fs.Close();

System.Text.Reg ularExpressions .Regex regex= new
System.Text.Reg ularExpressions .Regex (Input.Regexp);

foreach (TestCase tc in Input.TestList) {
System.Console. WriteLine(tc.In put +"\n " + tc.ExpectedOutp ut + " \\ " +
regex.IsMatch(t c.Input));
}
}
}
}

// This is input data. Store this in the XML file that is de-serialized for
this test.

<Email.Validati on.Input>
<TestList>
<!--
=============== =============== =============== =============== ====== -->
<!-- =============== ==== True test cases
=============== =============== -->
<!--
=============== =============== =============== =============== ====== -->
<Case Email="Ro***@ra bbit.com" Valid="true" />
<Case
Email="th****** *************** ************@so mething.org"
Valid="true" />
<Case Email="th****** *@something.9g" Valid="true" />
<Case Email="th****** *@place.org" Valid="true" />
<Case Email="We****** *****@cornell.e du" Valid="true" />
<Case Email="Ja****** *****@sun-east.com" Valid="true" />
<Case Email="Ja****** *****@sun.east. com" Valid="true" />
<Case Email="Ja****** *****@sun.com" Valid="true" />
<Case Email="Pr****** *@rolling-hills.club.org" Valid="true" />
<Case Email="9L****@c lub.org" Valid="true" />
<Case Email="fr**@som ewhere.org9" Valid="true" />
<Case Email="f@z.k" Valid="true" />
<Case Email="_e***@se same.org" Valid="true" />
<Case Email="Ha****** ****@Hogwarts.e du" Valid="true" />
<Case
Email="Pr****** *************** ***@Faculty.Hog warts.edu"
Valid="true" />

<!--
=============== =============== =============== =============== ====== -->
<!-- =============== ==== False="test cases
=============== ============== -->
<!--
=============== =============== =============== =============== ====== -->
<Case Email="-e***@sesame.org " Valid="false"/>
<Case Email="el**@ses ame.org." Valid="false" />
<Case Email="-e***@sesame.org ." Valid="false" />
<Case Email="elmo@.or g." Valid="false" />
<Case Email="elmo@.or g" Valid="false" />
<Case Email="elmo@.so meplace.org" Valid="false" />
<Case Email="elmo@clo ud9" Valid="false" />
<Case Email="fred.@so mewhere.org9" Valid="false" />
<Case Email="fred@som ewhere..org9" Valid="false" />
<Case Email="9Lives.c lub.org" Valid="false" />
<Case Email="@club.or g" Valid="false" />
<Case Email=".so***** @club.org" Valid="false" />
</TestList>

<Regexp>^([A-Za-z0-9]([\.\-A-Za-z0-9_]*[A-Za-z0-9])?)@([A-Za-z0-9]([\.\-A-Za
-z0-9_]*[A-Za-z0-9])*\.[A-Za-z0-9]([\.\-A-Za-z0-9_]*[A-Za-z0-9])?)$</Regexp>

<!--
<Regexp>^(\w([\.\-\w]*\w)?)@(\w([\.\-\w]*\w)*\.\w([\.\-\w]*\w)?)$</Regexp> -
->

</Email.Validatio n.Input>


"Brian Davis" <br***@knowdotn et.com> wrote in message
news:On******** ******@tk2msftn gp13.phx.gbl...


Try this one out:

(?i)^((?<!P\.?\ s?O\.?\sBox).)+ (?<!P\.?\s?O\.? \sBox)$

The (?i) turns on the ignore case option and then the expression matches the beginning of the string, followed by 1 or more characters that are not
preceded by P.O. Box, followed by the end of the string. The repeated
negative look-behind is there to make sure that a string containing only
"P.O. Box" is not matched.
Brian Davis
www.knowdotnet.com

"Bryce Budd" <bb***@fulltilt .com> wrote in message
news:0b******** *************** *****@phx.gbl.. .
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.

I need this validation to occur on the client, hence the
Regular Expression Validator control.

Here's the RE: I'm using in the ValidationExpre ssion
property:

[^(P\.?\s?O\.?\s Box)+]

This currently matches (returns false/invalid) for:
smith road and smith po box road?

Any insight into the proper regular expression to achieve
my goal would be greatly appreciated.
Thanks in advance,

Bryce


Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2214
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
2939
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
1
693
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.
2
2251
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
5572
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
9861
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
4463
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
1392
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
2251
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. ...
0
8468
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8901
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8660
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
7415
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
6213
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
4209
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
4390
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2041
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1792
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.