Web Studyaspnet.blogspot.com Babyneed.blogspot.com

ASP.Net Web Developer Guide

Power By: eXTReMe Tracker Powered by Blogger  
 
 
 
 

 

 
Important Links
 
   
     
 
Sitemap
 
   
   
 
Reference
 
   

Sunday, March 26, 2006

Validating Patterned Strings, Passwords, and Dates (Chapter 3)

Suppose that we want the user to enter the phone number, date of birth, hiredate, password, and confirmation of password. Also suppose that the business environment dictates that we enforce the following constraints:
  • The phone number must follow a pattern like (ddd)ddd-dddd for employees in the USA. It should match dd.dd.dd.dd for employees in France.

  • The date of birth must be between 1/1/1940 and 1/12/1985.

  • Hire date must be after the date of birth and before 6/15/2001.

  • The user should enter the password twice, and both entries must be identical.
We have developed an application to enforce these business rules.The output of the application is shown in Figure 3.64.
Figure 3.64 Validating Patterned Strings and Passwords


The complete code for this application is shown in Figure 3.65.We have enforced the underlying constraints as follows:
Constraint 1. We will use a regular expression to implement this constraint. The following regular expressions are identical. Both of these expressions will test the pattern (ddd)ddd-ddd:
ValidationExpression="\(\d\d\d\)\d\d\d\-\d\d\d\d">
ValidationExpression="\(\d{3}\)\d{3}\-\d{4}"
However, for French employees we must also test a pattern like dd.dd.dd.dd.The regular expression for this pattern would be this:
ValidationExpression="\d{2}\.\d{2}\.\d{2}\.\d{2}"
We may parenthesize these two expressions and connect them with a pipe ( ) symbol to specify that any one of the expressions needs to be satisfied, as follows:
ValidationExpression="(\(\d{3}\)\d{3}\\d{4})
(\d{2}\.\d{2}\.\d{2}\.\d{2})"
Constraint 2. We have used a RangeValidator to enforce this rule.
Constraint 3. We have used a combination of the CompareValidator and
the RangeValidator.The CompareValidator checks whether the date in txtDateHired is greater than that in txtDateOfBirth.The code for that is as follows:
Hire Date?
<asp:TextBox id="txtDateHired" rows="1" width="100" runat="server"/>
<asp:CompareValidator id="compDateHired" runat="server"
display="dynamic"
controlToValidate="txtDateHired"
controlToCompare="txtDateOfBirth"
errorMessage="Hire Date must be after Date of Birth"
type="String" operator="GreaterThan">
</asp:CompareValidator><br/>
The RangeValidator checks whether the date in txtDateHired is less than “6/15/2001.” The minimumValue is set to “1/1/1900” because the RangeValidator will not work unless both the minimumValue and maximumValue are both present.The code snippet follows:
<asp:RangeValidator id="ranvDateHired" runat="server" type="Date"
display="dynamic" controlToValidate="txtDateHired"
errorMessage="Hire date must be before 6/1/2001"
minimumValue="1/1/1900" maximumValue="6/15/2001" >
</asp:RangeValidator><br/>
Constraint 4. Two asp:TextBox controls have been used.The TextMode properties have been set to “Password”. CompareValidator has been attached to the txtConfirmPassword. Its ControlToCompare property has been set to “txtPassword.”:
controlToValidate="txtConfirmPassword" controlToCompare="txtPassword"
type="String" operator="Equal"
Figure 3.65 Validator9.aspx
<!—- Chapter3\Validator9.aspx —>
<html><head</head><body><form runat="server">
Phone Number? (ddd)ddd-dddd or dd.dd.dd.dd
<asp:TextBox id="txtPhone" rows="1 " width="100" runat="server"/>
<asp:RequiredFieldValidator id="validTxtName" runat="server"
controlToValidate="txtPhone" errorMessage="Name must be entered"
display="dynamic">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator id="regvPhone" runat="server"
display="dynamic" controlToValidate="txtPhone"
errorMessage="Incorrect Phone Number"
validationExpression=
"(\(\d{3}\)\d{3}\-\d{4})(\d{2}\.\d{2}\.\d{2}\.\d{2})">
</asp:RegularExpressionValidator><br>
Date of Birth? (mm/dd/yyyy) :
<asp:TextBox id="txtDateOfBirth" rows="1" width="100" runat="server"/>
<asp:RangeValidator id="ranvDob" runat="server" type="Date"
display="dynamic" controlToValidate="txtDateOfBirth"
errorMessage= "Must be within 1/1/1940 and 12/1/1985"
minimumValue="1/1/1940" maximumValue="12/1/1985">
</asp:RangeValidator></br>
Hire Date?
<asp:TextBox id="txtDateHired" rows="1 " width="100" runat="server"/>
<asp:CompareValidator id="compDateHired" runat="server"
display="dynamic" controlToValidate="txtDateHired"
controlToCompare="txtDateOfBirth"
errorMessage="Hire Date must be after Date of Birth" type="String"
operator="GreaterThan">
</asp:CompareValidator><br/>
<asp:RangeValidator id="ranvDateHired" runat="server" type="Date"
display="dynamic" controlToValidate="txtDateHired"
errorMessage="Hire date must be before 6/1/2001"
minimumValue="1/1/1900" maximumValue="6/15/2001" >
</asp:RangeValidator><br/>
Password?
<asp:TextBox id="txtPassword" textmode="password" width="100"
runat="server"/><br/>
Confirm Password:
<asp:TextBox id="txtConfirmPassword" textMode="password" width="100"
runat="server" />
<asp:CompareValidator id="comvConfirmPassword" runat="server"
display="static" controlToValidate="txtConfirmPassword"
controlToCompare="txtPassword"
errorMessage="Both passwords must be same" type="String"
operator="Equal">
</asp:CompareValidator><br/>
<asp:Button id="btnSubmit" runat="server" text="Submit"/> <br><br><br>
</form></body></html>

0 Comments:

Post a Comment

<< Home