Connecting Tech Pros Worldwide Forums | Help | Site Map

Regular Expression Validator

Bryce Budd
Guest
 
Posts: n/a
#1: Jul 21 '05
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 ValidationExpression
property:

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

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



Brian Davis
Guest
 
Posts: n/a
#2: Jul 21 '05

re: Regular Expression Validator




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" <bbudd@fulltilt.com> wrote in message
news:0b2201c37e04$76640060$a001280a@phx.gbl...[color=blue]
> 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 ValidationExpression
> property:
>
> [^(P\.?\s?O\.?\sBox)+]
>
> 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
>
>[/color]


Dino Chiesa [Microsoft]
Guest
 
Posts: n/a
#3: Jul 21 '05

re: Regular Expression Validator


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.EmailValidation {

/// <remarks>
/// Represents all the input for the test, including the regex to test,
/// and an array of test cases.
/// </remarks>
[System.Xml.Serialization.XmlRootAttribute("Email.V alidation.Input",
Namespace="", IsNullable=false)]
public class TestInput {

/// <remarks/>

[System.Xml.Serialization.XmlElementAttribute(Form= System.Xml.Schema.XmlSche
maForm.Unqualified)]
public string Regexp;

/// <remarks/>

[System.Xml.Serialization.XmlArrayAttribute(Form=Sy stem.Xml.Schema.XmlSchema
Form.Unqualified)]
[System.Xml.Serialization.XmlArrayItemAttribute("Ca se",
Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
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.Serialization.XmlAttribute("Email",
Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Input;

/// <remarks/>
[System.Xml.Serialization.XmlAttribute("Valid",
Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
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= "EmailValidationInput.xml";

System.IO.FileStream fs = new System.IO.FileStream(InputPath,
System.IO.FileMode.Open);
System.Xml.Serialization.XmlSerializer s= new
System.Xml.Serialization.XmlSerializer(typeof(Test Input));
TestInput Input= (TestInput) s.Deserialize(fs);
fs.Close();

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

foreach (TestCase tc in Input.TestList) {
System.Console.WriteLine(tc.Input +"\n " + tc.ExpectedOutput + " \\ " +
regex.IsMatch(tc.Input));
}
}
}
}



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

<Email.Validation.Input>
<TestList>
<!--
================================================== ================ -->
<!-- =================== True test cases
============================== -->
<!--
================================================== ================ -->
<Case Email="Roger@rabbit.com" Valid="true" />
<Case
Email="this.is.a.really.long.email.address@somethi ng.org"
Valid="true" />
<Case Email="this.name@something.9g" Valid="true" />
<Case Email="this.name@place.org" Valid="true" />
<Case Email="Werner-vogels@cornell.edu" Valid="true" />
<Case Email="James.Gosling@sun-east.com" Valid="true" />
<Case Email="James.Gosling@sun.east.com" Valid="true" />
<Case Email="James.Gosling@sun.com" Valid="true" />
<Case Email="President@rolling-hills.club.org" Valid="true" />
<Case Email="9Lives@club.org" Valid="true" />
<Case Email="fred@somewhere.org9" Valid="true" />
<Case Email="f@z.k" Valid="true" />
<Case Email="_elmo@sesame.org" Valid="true" />
<Case Email="Harry_Potter@Hogwarts.edu" Valid="true" />
<Case
Email="Professor.Angus_Dumbledore@Faculty.Hogwarts .edu"
Valid="true" />

<!--
================================================== ================ -->
<!-- =================== False="test cases
============================= -->
<!--
================================================== ================ -->
<Case Email="-elmo@sesame.org" Valid="false"/>
<Case Email="elmo@sesame.org." Valid="false" />
<Case Email="-elmo@sesame.org." Valid="false" />
<Case Email="elmo@.org." Valid="false" />
<Case Email="elmo@.org" Valid="false" />
<Case Email="elmo@.someplace.org" Valid="false" />
<Case Email="elmo@cloud9" Valid="false" />
<Case Email="fred.@somewhere.org9" Valid="false" />
<Case Email="fred@somewhere..org9" Valid="false" />
<Case Email="9Lives.club.org" Valid="false" />
<Case Email="@club.org" Valid="false" />
<Case Email=".someone@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.Validation.Input>




"Brian Davis" <brian@knowdotnet.com> wrote in message
news:OnRuX0gfDHA.3528@tk2msftngp13.phx.gbl...[color=blue]
>
>
> 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[/color]
the[color=blue]
> 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" <bbudd@fulltilt.com> wrote in message
> news:0b2201c37e04$76640060$a001280a@phx.gbl...[color=green]
> > 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 ValidationExpression
> > property:
> >
> > [^(P\.?\s?O\.?\sBox)+]
> >
> > 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
> >
> >[/color]
>
>[/color]


Closed Thread