how to use the chapter 7 validation controls€¦ · murach's asp.net 4.5/c#, c7 © 2013, mike...

35
Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Upload: others

Post on 15-Oct-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1

Chapter 7

How to use the validation controls

Page 2: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 2

The validation controls provided by ASP.NET RequiredFieldValidator

CompareValidator

RangeValidator

RegularExpressionValidator

CustomValidator

ValidationSummary

Page 3: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 3

Typical code for processing a page that contains validation controls

protected void btnAdd_Click(object sender, EventArgs e)

{

if (Page.IsValid)

{

// code for processing the valid data

}

}

Page 4: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 4

Common validator attributes and properties

ControlToValidate

Display

Text

ErrorMessage

Enabled

EnableClientScript

SetFocusOnError

ValidationGroup

Page 5: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 5

The Behavior category in the Properties window for a Compare validator

Page 6: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 6

A validator that checks for a required entry <asp:TextBox ID="txtName" runat="server">

</asp:TextBox>&nbsp;

<asp:RequiredFieldValidator

ID="RequiredFieldValidator1"

runat="server"

ControlToValidate="txtName"

ErrorMessage="You must enter a name." >

</asp:RequiredFieldValidator>

Page 7: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 7

A property of the required field validator

InitialValue

A validator that checks that an initial value is changed

<asp:TextBox ID="txtBirthDate" runat="server>mm/dd/yyyy

</asp:TextBox>&nbsp;

<asp:RequiredFieldValidator

ID="RequiredFieldValidator2"

runat="server"

ControlToValidate="txtBirthDate"

InitialValue="mm/dd/yyyy"

ErrorMessage="You must enter a birthdate.">

</asp:RequiredFieldValidator>

Page 8: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 8

A required field validator that forces an option to be chosen from a list box

<asp:ListBox ID="lstCardType" runat="server">

<asp:ListItem Selected="True" Value="None">

Select a credit card</asp:ListItem>

<asp:ListItem Value="Visa">Visa</asp:ListItem>

<asp:ListItem Value="MC">MasterCard</asp:ListItem>

<asp:ListItem Value="AmEx">American Express

</asp:ListItem>

</asp:ListBox>&nbsp;

<asp:RequiredFieldValidator

ID="RequiredFieldValidator3" runat="server"

ControlToValidate="lstCardType"

InitialValue="None"

ErrorMessage="You must select a credit card type.">

</asp:RequiredFieldValidator>

Page 9: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 9

Properties of the compare validator ValueToCompare

Operator

Type

ControlToCompare

A compare validator that checks for a value greater than zero

<asp:TextBox ID="txtQuantity" runat="server">

</asp:TextBox>&nbsp;

<asp:CompareValidator ID="CompareValidator1"

runat="server"

ControlToValidate="txtQuantity"

Type="Integer"

Operator="GreaterThan"

ValueToCompare="0"

ErrorMessage="Quantity must be greater than zero.">

</asp:CompareValidator>

Page 10: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 10

A compare validator that checks for an integer value

<asp:TextBox id="txtQuantity" runat="server">

</asp:TextBox>&nbsp;

<asp:CompareValidator ID="CompareValidator2"

runat="server"

ControlToValidate="txtQuantity"

Operator="DataTypeCheck"

Type="Integer"

ErrorMessage="Quantity must be an integer.">

</asp:CompareValidator>

Page 11: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 11

A compare validator that compares the values of two text boxes

<asp:TextBox ID="txtStartDate" runat="server">

</asp:TextBox><br /><br />

<asp:TextBox ID="txtEndDate" runat="server">

</asp:TextBox>&nbsp;

<asp:CompareValidator

ID="CompareValidator3" runat="server"

ControlToValidate="txtEndDate"

Operator="GreaterThan"

Type="Date"

ControlToCompare="txtStartDate"

ErrorMessage=

"End Date must be greater than Start Date.">

</asp:CompareValidator>

Page 12: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 12

Properties of the range validator

MinimumValue

MaximumValue

Type

A range validator that checks for a numeric range <asp:TextBox ID="txtDays" runat="server">

</asp:TextBox>&nbsp;

<asp:RangeValidator ID="RangeValidator1" runat="server"

ControlToValidate="txtDays"

Type="Integer"

MinimumValue="1"

MaximumValue="14"

ErrorMessage="Days must be between 1 and 14.">

</asp:RangeValidator>

Page 13: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 13

How to set a range at runtime

A validator that checks a date range that’s set at runtime

<asp:TextBox ID="txtArrival" runat="server">01/01/12

</asp:TextBox>&nbsp;

<asp:RangeValidator ID="valArrival" runat="server"

ControlToValidate="txtArrival"

Type="Date"

ErrorMessage="You must arrive within 30 days.">

</asp:RangeValidator>

Code that sets the min and max values

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

valArrival.MinimumValue =

DateTime.Today.ToShortDateString();

valArrival.MaximumValue =

DateTime.Today.AddDays(30).ToShortDateString();

}

}

Page 14: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 14

A property of the regular expression validator

ValidationExpression

A regular expression validator that validates five-digit numbers

<asp:TextBox ID="txtZipCode" runat="server">

</asp:TextBox>&nbsp;

<asp:RegularExpressionValidator

ID="RegularExpressionValidator1"

runat="server" ControlToValidate="txtZipCode"

ValidationExpression="\d{5}"

ErrorMessage="Must be a five-digit U.S. zip code.">

</asp:RegularExpressionValidator>

Page 15: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 15

The Regular Expression Editor dialog box

Page 16: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 16

A regular expression validator that validates U.S. phone numbers

<asp:TextBox ID="txtPhone" runat="server">

</asp:TextBox>&nbsp;

<asp:RegularExpressionValidator

ID="RegularExpressionValidator2"

runat="server" ControlToValidate="txtPhone"

ValidationExpression=

"((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}"

ErrorMessage="Must be a valid U.S. phone number.">

</asp:RegularExpressionValidator>

Page 17: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 17

Common regular expression elements Ordinary character

\

\d

\D

\w

\W

\s

\S

[abcd]

[^abcd]

[a-z]

{n}

{n,}

{n,m}

*

?

+

|

( )

Page 18: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 18

Examples of regular expressions

Expression Example

\d{3} 289

\w{8,20} Frankenstein

\d{2}-\d{4} 10-3944

\w{1,8}.\w{1,3} freddy.jpg

(AB)|(SB)-\d{1,5} SB-3276

\d{5}(-\d{4})? 93711-2765

\w*\d\w* arm01

[xyz]\d{3} x023

Page 19: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 19

Properties of the validation summary control

DisplayMode

HeaderText

ShowSummary

ShowMessageBox

Page 20: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 20

The aspx code for a validation summary control and two validators

<asp:ValidationSummary ID="ValidationSummary1"

runat="server"

HeaderText="Please correct these entries"

BorderColor="Black"

BorderStyle="Solid" BorderWidth="1px" />

<h2>Contact information</h2>

<label>Email address: </label>

<asp:TextBox ID="txtEmail1" runat="server"

CssClass="entry">

</asp:TextBox>

<asp:RequiredFieldValidator ID="rfvEmail1"

runat="server" CssClass="validator"

Display="Dynamic" ControlToValidate="txtEmail1"

ErrorMessage="First email address">

Email is required

</asp:RequiredFieldValidator><br />

Page 21: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 21

The aspx code (continued) <label>Email Re-entry: </label>

<asp:TextBox ID="txtEmail2" runat="server"

CssClass="entry">

</asp:TextBox>

<asp:CompareValidator ID="cvEmail2" runat="server"

ErrorMessage="Second email address"

ControlToCompare="txtEmail1"

ControlToValidate="txtEmail2"

CssClass="validator">

Must match first entry

</asp:CompareValidator><br />

Page 22: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 22

How the error messages appear on the web page

Page 23: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 23

The property for working with validation groups

ValidationGroup

Page 24: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 24

Part of a web form that accepts billing and shipping addresses

Page 25: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 25

A text box with a validator that specifies a validation group

The aspx code

<asp:TextBox ID="txtShipToAddress" runat="server" />

<asp:RequiredFieldValidator ID="RequiredFieldValidator1"

runat="server"

ControlToValidate="txtShipToAddress"

ErrorMessage="You must enter a shipping address."

ValidationGroup="ShipTo">

</asp:RequiredFieldValidator>

A button that starts the validation of the group

<asp:Button ID="btnContinue" runat="server"

Text="Continue"

ValidationGroup="ShipTo"

OnClick="btnContinue_Click" />

C# code that conditionally validates the group

if (!chkShipToSameAsBillTo.Checked)

Page.Validate("ShipTo");

Page 26: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 26

The Check Out page with validation

Page 27: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 27

The start of the aspx code for the CheckOut page <h1>CheckOut Page 1</h1>

<form id="form1" runat="server" defaultfocus="txtEmail1"

defaultbutton="btnCheckOut">

<asp:ValidationSummary ID="ValidationSummary1"

runat="server"

HeaderText="Please correct these entries"

BorderColor="Black"

BorderStyle="Solid" BorderWidth="1px" />

<h2>Contact information</h2>

Page 28: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 28

The start of the aspx code (cont.) <label>Email address: </label>

<asp:TextBox ID="txtEmail1" runat="server"

CssClass="entry">

</asp:TextBox>

<asp:RequiredFieldValidator ID="rfvEmail1"

runat="server"

CssClass="validator"

ErrorMessage="First email address"

Display="Dynamic"

ControlToValidate="txtEmail1" >Required

</asp:RequiredFieldValidator>

<asp:RegularExpressionValidator ID="revtxtEmail1"

runat="server"

ErrorMessage="First email address"

CssClass="validator"

Display="Dynamic"

ValidationExpression=

"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"

ControlToValidate="txtEmail1">

Must be a valid email address

</asp:RegularExpressionValidator><br />

Page 29: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 29

The start of the aspx code (cont.) <label>Email re-entry: </label>

<asp:TextBox ID="txtEmail2" runat="server"

CssClass="entry">

</asp:TextBox>

<asp:RequiredFieldValidator ID="rfvEmail2"

runat="server"

ErrorMessage="Second email address"

CssClass="validator"

Display="Dynamic"

ControlToValidate="txtEmail2">Required

</asp:RequiredFieldValidator>

<asp:CompareValidator ID="cvEmail2" runat="server"

ErrorMessage="Second email address"

ControlToCompare="txtEmail1"

ControlToValidate="txtEmail2"

CssClass="validator"

Display="Dynamic">Must match first entry

</asp:CompareValidator><br />

Page 30: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 30

The start of the aspx for the shipping-address <h2>Shipping address</h2>

<div id="sameAsBilling">

<asp:CheckBox ID="chkSameAsBilling" runat="server"

Text="Same as billing address" AutoPostBack="True"

OnCheckedChanged=

"chkSameAsBilling_CheckedChanged"/>

<br /><br />

</div>

<label>Address: </label>

<asp:TextBox ID="txtShipAddress" runat="server"

CssClass="entry">

</asp:TextBox>

<asp:RequiredFieldValidator ID="rfvShipAddress"

runat="server"

ErrorMessage="Shipping address"

CssClass="validator" Display="Dynamic"

ControlToValidate="txtShipAddress"

Text="Required">

</asp:RequiredFieldValidator><br />

Page 31: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 31

The aspx for the shipping-address (cont.) <label>City: </label>

<asp:TextBox ID="txtShipCity" runat="server"

CssClass="entry" >

</asp:TextBox>

<asp:RequiredFieldValidator ID="rfvShipCity"

runat="server"

ErrorMessage="Shipping city" CssClass="validator"

Display="Dynamic" ControlToValidate="txtShipCity"

Text="Required">

</asp:RequiredFieldValidator><br />

Page 32: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 32

Some of the C# code in the code-behind file public partial class CheckOut : System.Web.UI.Page

{

private Customer customer;

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

customer = (Customer)Session["Customer"];

this.LoadCustomerData();

}

}

protected void btnCheckOut_Click(

object sender, EventArgs e)

{

if (Page.IsValid)

{

this.GetCustomerData();

Response.Redirect("~/CheckOut2.aspx");

}

}

Page 33: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 33

Some of the code in the code-behind file (cont.) private void GetCustomerData()

{

if (customer == null)

customer = new Customer();

// statements that get the data

// from the billing fields

customer.ShippingAddress = txtShipAddress.Text;

customer.ShippingCity = txtShipCity.Text;

customer.ShippingState =

ddlShipState.SelectedValue;

customer.ShippingZip = txtShipZip.Text;

Session["Customer"] = customer;

}

Page 34: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 34

Some of the code in the code-behind file (cont.) protected void chkSameAsBilling_CheckedChanged(

object sender, EventArgs e)

{

if (!chkSameAsBilling.Checked) {

rfvShipAddress.Enabled = true;

rfvShipCity.Enabled = true;

rfvShipState.Enabled = true;

rfvShipZip.Enabled = true;

txtShipAddress.Enabled = true;

txtShipCity.Enabled = true;

ddlShipState.Enabled = true;

txtShipZip.Enabled = true;

}

else {

rfvShipAddress.Enabled = false;

rfvShipCity.Enabled = false;

...

ddlShipState.Enabled = false;

txtShipZip.Enabled = false;

}

}

Page 35: How to use the Chapter 7 validation controls€¦ · Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 1 Chapter 7 How to use the validation controls

Murach's ASP.NET 4.5/C#, C7 © 2013, Mike Murach & Associates, Inc. Slide 35

Extra 7-1 Add validation to the Reservation form