Multiple Validators on a Single Field

Loading the ValidatorTest.aspx page from Listing 5-4 and clicking Validate displays the page shown in Figure 5-8.

^Bii - ■+ ■ J J ^ iS^fa^ jJFwt« JrVita J -J)- Ji

Lrtj ™

Validator Test Page

IkvVrfdtfMO-U) |

cii^ Vjlid/ihi.n i.r_r_r. iji nrx_r.;


Valid alt |

H jqc- v'ji'ld


¿Da* ¿il«^rtarrt

Figure 5-8 : The page that results from clicking the Validate button with no values filled in Notice that it displays the message "Page Valid". This is almost certainly not what you wanted! You specified that the first field should be a number between 1 and 12, that the second field should be some string that looks like a social security number, and that the last field should be "Hello". But it turns out that, by design, all validators except RequiredFieldValidator don't validate against an empty control. There must be a solution. One solution is to use the RequiredFieldValidator control. Listing 5-5 shows a modified version of ValidatorTest.aspx, named ValidatorTestRequired.aspx. The difference between ValidatorTest.aspx (which is shown in Listing 5-4) and

ValidatorTestRequired.aspx is the addition of a RequiredFieldValidator control for each of the fields covered by another validator. Listing 5-5 ValidatorTestRequired.aspx, a page that requires all fields to be filled, with valid data valid data

<script language="C#" runat=server>

void Validate_Click(Object sender, EventArgs E) {

Msg.Text="Page Valid";

void CustomServerVal (object source, ServerValidateEventArgs args) {

Msg.Text="ServerValidation called and TRUE returned."; args.IsValid=true;

Msg.Text="ServerValidation called and FALSE returned."; args.IsValid=false;

catch {

Msg.Text="ServerValidation called and FALSE returned."; args.IsValid=false;

<body> <form runat=server> <center> <h3>

<font face="Verdana" color=blue>

Validator Test Page - Required Entry </font> </h3> <table> <tr> <td>

<input id="Range"


<ASP:RangeValidator ID="ValRange"






ErrorMessage="Out of Range"

runat=server />

<ASP:RequiredFieldValidator ControlToValidate="Range" Display="Dynamic" ErrorMessage="Must enter a value." runat=server />

Regular Expression Validation (nnn-nn-nnnn): </td> <td>

<input id="RegEx"


<ASP:RegularExpressionValidator ID="ValRegEx" ControlToValidate="RegEx" runat="SERVER" Display="Dynamic" ErrorMessage=

ValidationExpression= "[0-9]{3}-[0-9] {2}-[0-9]{4}" /> <ASP:RequiredFieldValidator ControlToValidate="RegEx" Display="Dynamic" ErrorMessage="Must enter a value." runat=server />

Custom Validation (It wants you to enter "Hello" WITHOUT THE QUOTES): </td> <td>

<input type="text" id="txtCustom" runat=server size=11 /> </td> <td>

<ASP:CustomValidator ID="ValCustom" runat="server"

ControlToValidate="txtCustom" OnServerValidate="CustomServerVal"

Display="Dynamic" >

Enter "Hello". Case-Sensitive. </ASP:CustomValidator>

<ASP:RequiredFieldValidator ControlToValidate="txtCustom" Display="Dynamic" ErrorMessage="Must enter a value." runat=server />

<td colspan=3 align="center"> <asp:button text="Validate" OnClick="Validate_Click" runat=server>

<asp:Label id="Msg" ForeColor="red" Font-Name="Verdana" Font-Size="10" runat=server /> </td> </tr> </table> </center> </form> </body> </html>

One attribute of all validators we haven't yet looked at is the Display attribute. The Display attribute expects one of three values: None, Static, or Dynamic. When the

Display attribute is set to None, the validation error message isn't displayed. When the Display attribute is set to Static, the layout of the page won't change when the validator control displays an error message. In this case, the validator contents are physically part of the page, and space will be allocated for them in the layout. When Display is set to Dynamic, the validator output is not part of the page until it's displayed. In Listing 5-5, I set each validator's Display attribute to Dynamic. Using Dynamic has the unfortunate effect of possibly causing the layout of the page to change when validators are fired, but the result is generally better than setting Display to Static. For example, when I changed RangeValidatorto Static and then submitted the form with no value in the Range field, the page shown in Figure 5-9 was displayed.

FI'IWftffBMff MWffR'f ■I'J.iJlll.l.i.ii ' .!■ -B--.. - ftJJL

ft EJH tttf Iert tht


Validator Test Pag« - Required Entry Eip^fjun V.iLdilitii ira-rai-niwfl}

Qiftiiu VjuJjtdii w»ii3 yvju-bt- tidti 'EM-..' i 'WITHOUT THE QUOTES) 1

0 0

Post a comment