ASP.NET Validation Controls Part 2

Validation controls are simply placed on your web page and associated with another control such as a textbox or a dropdownlist.

For example suppose you placed a textbox control on your web page to retrieve a user’s email address, you could also place a RequiredFieldValidator and associate the RequiredFieldValidator with the textbox control to ensure the user enters some value before clicking the form submission button.

Not only could you use a RequiredFieldValidator you could also associate a RegularExpressionValidator with the textbox to ensure that the value entered matched a correctly formatted email address.

Please note that although some of the codesnippets in the following pages leave out the form runat="server" tag for brevity, it's a must that your validation controls are enclosed by the form element to work as the codesnippet below demonstrates.

<form id="form1" runat="server">

<asp:Textbox id="email" runat="server" />
<asp:RequiredFieldValidator id="rfv_Email" ControlToValidate="email" Text="You must insert an email address" runat="server" />

</form>

Clientside and Serverside validation

Validation controls are by default set to run on both the clientside i.e. in the user’s browser and on the server when the form is posted back to the server. Browsers such as IE, Firefox and Opera all support clientside Javascript, nevertheless in case the user is using an older browser or has disabled Javascript the validation controls will run on the server as well.

Even when client validation is run and the user’s input valid, validation is still performed on the server for security reasons.

There is the option to disable clientside validation for any of the validation controls by setting their EnableClientScript property to false.

Read Part 1 Validation Controls

Read Part 3 Validation Control Properties

Advertisements



MembersPro

MembersPro PayPal - ASP Membership software

Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.

Get your best asp web hosting provider now and save 25%