Page 73 - DCAP312_WEB_TECHNOLOGIES_II
P. 73

Unit 4: Advanced Server Controls



               3.  RangeValidator                                                                 Notes
               4.  RegularExpressionValidator
               5.  CustomValidator

               6.  Validation Summary
            We can also customize validation for our own needs. Then, if there are any errors in the form
            data, these validation server controls enable us to customize the display of error information
            on the browser.

            We place validation server controls on our page as we would any other type of controls. After the
            user submits the form, the user’s form information is sent to the appropriate validation control,
            where it is evaluated. If the information does not validate, the control sets a page property that
            indicates this. After all the form information is sent to all the validation server controls, if one
            or more of the validation server controls cannot validate the information sent to it, the entire
            form input is found to be invalid, and the user is notified.(See Table 4.2)

                               Table 4.2: Available validation server controls

                 Validation Server Control                Description
                RequiredFieldValidator    Ensures that the user does not skip a form entry field
                CompareValidator          Allows for comparisons between the user’s input and
                                          another item using a comparison operator (equals,
                                          greater than, less than
                RangeValidator            Checks  the  user’s  input  based  upon  a  lower-  and
                                          upper-level range of numbers or characters
                RegularExpressionValidator  Checks that the user’s entry matches a pattern defined
                                          by a regular expression. This is a good control to use
                                          to check e-mail addresses and phone numbers
                CustomValidator           Checks the user’s entry using custom-coded
                                          validation logic
                Validation Summary        Displays all the error messages from the valuators
                                          in one specific spot on the page


            The RequiredFieldValidator Control
            The RequiredFieldValidator server control makes sure that the user enters something into the
            field that it is associated with in the form. We need to tie the RequiredFieldValidator to each
            control that is a required field in the form. Although this is the simplest of the validation server
            controls, we must understand certain things about it.

            To  see  an  example  of  using  the  RequiredFieldValidator  server  control,  create  a  Web  form
            that contains a Textbox server control and a Button server control. Next to the button, place a
            RequiredFieldValidator server control. Our ASP.NET page should look like the code illustrated
            in Listing 2. (See Figure 4.2)
                           Using the RequiredFieldValidator control
                           Visual C# .NET
                           <%@ Page Language= “VB” %>
                           <script runat= “server”>
                           Sub Button1_Click (sender As Object, e As EventArgs)
                           Label1.Text = “Page is valid!”


                                             LOVELY PROFESSIONAL UNIVERSITY                                    67
   68   69   70   71   72   73   74   75   76   77   78