Click here for this month's special
09.22.03


By Roger McCook

A Vision
The job given me by the Almighty Programmer was gatekeeper. The clouds parted below me and I could see a long sinewy line of expressions marching toward me in single file. Some looked like dates, others like digits and some (to be honest) looked like gibberish. One by one, they would try to get past me but I know no fear - for I am the RegularExpressionValidator.

The RegularExpressionValidator Control
The purpose of the RegularExpressionValidator control is to filter out unwanted or invalid input. It can be the first line of defense against input that is not formatted the way you want to see it. Correctly formatted expressions get through with no fanfare. Exceptions, however, cause the control to display its error message and the postback process is halted.

To demonstrate how this control works, let's drop a few controls on a web form. I'm using Visual Studio .NET with code-behind and my language is VB. I've set my form to Flow Control but all that isn't really necessary to understand the principles involved. Visual Studio has declared my control automatically with the following statement:
Protected WithEvents
RegularExpressionValidator1 As System.Web.UI.WebControls.RegularExpressionValidator

The rest is as easy as 1,2,3.

1. First I drop a text box on my form. This is where I enter my test expression. I'll give the control an id of "txtText".
<asp:TextBox id="txtTest" runat="server"> </asp:TextBox>
2. Next I drop a RegularExpressionValidator control on the form. I give this control an id of "RegularExpressionValidator1" since I'm not feeling very creative today. I set the "ControlToValidate" property to the id of the control I want to validate (which is the textbox I just created). I set the "ErrorMessage" property to what I want to display if the expression is not valid. I set the "ValidExpression" property to a regular expression that I want to use to compare against the input. In this case, I've just set the expression to the letter "m".
<asp:RegularExpressionValidator id="RegularExpressionValidator1" 
runat="server" ControlToValidate="txtTest" 
ErrorMessage="Sorry. You are not a valid expression!" 
ValidationExpression="m"> </asp:RegularExpressionValidator>
3. Next I drop a button control on the form. This is just so we have a place for the cursor to go after we leave the textbox.
<asp:Button id="Button1" runat="server" 
                  Text="Submit"> </asp:Button>

Now we are ready to test our control that should only allow the letter "m" (lower case) to get by. If we enter "m" and press TAB or click the SUBMIT button, everything looks good - no error messages. If we leave the textbox blank, there is no error message either because the control only checks input that exists, not the existence of input. If, however, we enter any other character or digit the conrol's error message will be displayed. In this case, the error message is "Sorry. You are not a valid expression!"

Now that we know how to use the control, what we really need is a primer on regular expressions. Then we can easily drop a RegularExpressionValidator control on our form, associate it with an entry in a text box and set the "ValidationExpression" property to what we want the input to look like.

Regular Expressions
The subject of regular expressions is often confusing but we are going to take an approach that will give you a basic understanding upon which you can build. Becoming proficient with regular expressions takes a lot of practice, just like anything else.

Regular expressions let us to search for certain patterns. In the case of our validator control, if we find the pattern in the textbox, the control is satisfied and let's the text through to the promise land. If we do NOT find that pattern, the control is not happy and displays its error message. (This is not all we can do with regular expressions. We can also "search and replace" and reformat text. Just be aware that you can use regular expressions to find all instances of some word or phrase and replace it with another or reformat it. You can use them to mine documents for email addresses or URL's. I'll discuss that more fully in a future article.)


Character Matching
The easiest kind of matching we can do is "character matching". That's what we did when we put the letter "m" in our validator control. We are simply asking if the text is the letter "m" and nothing else. So the lowercase letter "m" passes but the uppercase "M" does not. If you enter "mom", it does not pass because "mom" is not "m" - it is something more than just "m".

Character matching is not limited to a single character. If you use the regular expression "mom" then the only expression that matches will be "mom". "MOM" will not work, "mommy" will not work and "I want my mom" will not work because none of these expressions is exactly equivalent to "mom". OK, I think we are clear on that point.

A period is a special character that matches any single character. So the regular expression "m.m" would match "mom" or "mam" or "m9m".

need help asap with asp!!!!
Hi, I created a search page, search.asp for a website http://www.patentlawbooks.com but I cannot make it work.

My database name is 'bookstore' my table name is 'table2'. It is a dsn less connection. The code for the search page is : ...

 Posted by: Cecile hoffman View Post  |  Click To Comment

You can search for a string that has a single character from a group of predetermined characters. For example: "m[ao]m" would match up with "mam" or "mom" because the middle letter is in the group [ao], but "m9m" would not match because "9" is not an "a" or an "o".

You can search for a string that has a single character that is in a range. For example: "m[a-y]m" will match up with "mam" or "mbm" or "mcm" or any other letter in the middle so long as it is in the range [a-y]. The letter "z" is not in that range and all uppercase letters are not in that range.

Click Here to Read the Full Article


About the Author:
Visit Roger McCook's Web site at http://www.mccooksoftware.com.

Read this newsletter at: http://www.webproasp.com/2003/0922.html

 

 

 

 

 

 

 

 

 

-- WebProAsp is an iEntry, Inc. publication --
2003 iEntry, Inc.  All Rights Reserved  Privacy Policy  Legal