To read the article online, visit http://www.4GuysFromRolla.com/webtech/041601-1.4.shtml

Preparing your ASP Pages for a Transition to ASP.NET, Part 4

By Darren Neimke and Scott Mitchell


  • Read Part 1
  • Read Part 2
  • Read Part 3

  • In Part 3 we looked at how to populate the blnIsValid and blnIsPostBack variables in our ASP code template. In this part we'll look at the validator functions we need in order to successfully determine whether or not blnIsValid should be True or False. We'll also look at an example application of this code template and wrap up the article!

    Creating the Validator Functions
    So far I (Darren Neimke) have written three specific validator functions that I use in this code template. After creating these functions be sure to copy them to an include file, Validators.asp. (To learn more about server-side includes be sure to read: The Low-Down on #include.) You are invited to create your own validation functions for this ASP code template. These three validation functions should get you started:

    '*******************************************************************
    ' The next 3 functions are Validation functions and should actualy be 
    ' removed to an include file called Validators.inc.
    '*******************************************************************
    
    Private function RequiredFieldValidator (val, name)
    ' This function validates that the passed-in field has a value.
    
      RequiredFieldValidator = ""
    	
      if isNull(val) or Len(Trim(val)) = 0 then
      ' If no value was entered for this field pass back an error message.
          RequiredFieldValidator = "You did not enter a value for " & _
                                   name & ".<br />" & vbCrLf
      end if
    end function
    
    
    
    Private function RegularExpressionValidator (val, name, pattern)
    ' This function validates that the nominated fields against a Regular
    ' Expression pattern.
    
      RegularExpressionValidator = ""
        
      ' Declare our variable.
      Dim objRE
    
      ' Create an instance of the regexp object.
      Set objRE = New regexp
    	
      ' Set the properties of the RegExp object.
      objRE.Pattern = pattern
      objRE.Global = True
      objRE.IgnoreCase = True
    	
      objRE.Test ( val )
    	
      if not objRE.Test ( val ) then
      ' The data entered does not match the required pattern.
         RegularExpressionValidator = "The data that you entered for " & _
              name & " does not match the required pattern.<br />" & vbCrLf
      end if
        
      ' Explicitly set our object to Nothing.
      Set objRE = Nothing
    end function
    
    
    
    Private Function RangeValidator (val, name, lower, upper)
    ' This function validates that the nominated fields have a value 
    ' between 2 Bounds.
    
       RangeValidator = ""
    	
      if not isNull(val) AND isNumeric(val) then
        if CLng(val) < lower or CLng(val) > upper then
        ' If no value was entered for this field pass back an error message.
          RangeValidator = "The value you entered for " & name & _
                         " was not within the accepted range of " & lower & _
                         " - " & upper & ".<br />" & vbCrLf
    	end if
      else
        RangeValidator = "You must enter a numeric value for  " & name & _
                         ".<br />" & vbCrLf
      end if
    end function
    

    Take a moment to check out this example script. The example encorporates all of the elements of the migration code template we've discussed in this article. As you poke through the example note that it is comprised of the following "sections:"

    1. Global variable definitions - At the beginning of the example, a number of global variables are defined. These include the blnIsPostback and blnIsValid variables that we discussed earlier as well as a variable for each form field.
    2. The inclusion of Validators.asp - this include file contains the validator functions, which are used in the ValidateFormValues function.
    3. The blnIsPostback and blnIsValid tests - These code blocks are nearly identical to the examples we looked at earlier in this article for determining the values of these two properties. It simply determines whether or not the page has been posted back to and, if it has, checks to see if all the validation controls are valid.
    4. The Page_Load function - Note that this function checks both the blnIsPostback and blnIsValid properties and can take various actions based on the values of these properties.
    5. The ValidateFormValues function - This function needs to be altered for each page, depending on the validation requirements for each form field value.
    6. The call to the Page_Load function - Note the Call Page_Load() line of code - this fires off the whole pseudo-event life-cycle of the code template page.
    7. A postback form - A postback form can be created by simply leaving off the ACTION parameter in the HTML FORM tag.

    That's it! I would recommend that you adopt a template similar to this when creating classic ASP pages. Doing so will make the migration to ASP.NET that much easier, both from a code maintenance and code style point of view. When I say code style point of view, what I mean is that you will already be thinking in terms of coding against the Page object and its events, the style employed by ASP.NET. Adopting this template will save you time when you decide to migrate your Web site to ASP.NET and will help gently familiarize you with some of ASP.NET's stylistic and code differences.

    Happy Programming!

  • By Darren Neimke and Scott Mitchell


    Attachments:

  • View the example code template
  • Visit the ASP.NET Article Index


  • Article Information
    Article Title: Preparing your ASP Pages for a Transition to ASP.NET, Part 4
    Article Author: Darren Neimke and Scott Mitchell
    Published Date: Monday, April 16, 2001
    Article URL: http://www.4GuysFromRolla.com/webtech/041601-1.4.shtml


    Copyright 2017 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers