iASP_Grid Component: Sample 4

Run Sample 4

NOTE: If you haven't configured your Microsoft Access driver, please click here.

This sample demonstrates the steps to perform input validation for a table field.  

The above screen shot shows a JavaScript message box which prompts for confirmation when a Delete button is clicked. This input validation is performed using JavaScript.

If the user tries to save a record after entering a phone number, with number of digits less or greater than 10, the user will be redirected to the following error page:

This input validation is performed using server side ASP script.

Sample code

<%
   Set Grid = Server.CreateObject("Persits.AspGrid")
   Grid.FileName = "Sample5.asp"
   Grid.Connect "sun.jdbc.odbc.JdbcOdbcDriver", "jdbc:Odbc:GridTest", "", ""
   Grid.SQL = " select id, name, phone from Departments"
   Grid.Cols(1).Hidden = True
   Grid.DeleteButtonOnClick = "if( confirm('Are you sure you want to delete this
      record?') == true ) this.form.submit(); return true;"
   Grid.UseImageButtons = False

   ' Validate Phone
   If Request("AspGridSave1") <> "" or Request("AspGridInsert1") <> "" Then
      Phone = Request("Field2")
      Count = 0
      For i = 1 to Len(Phone)
         if Mid(Phone, i, 1) >= "0" and Mid(Phone, i, 1) <= "9" Then Count = Count + 1
      Next
      if Count <> 10 Then
         Response.Redirect "Sample4Error.asp"
      End If
   End If
%>

<HTML>
<BODY>

<% Grid.Display %>
<% Grid.Disconnect %>

</BODY>
</HTML>


Description

The first five steps for creating a Grid Object have already been discussed. See the main page of samples for details.

Grid.DeleteButtonOnClick = "if( confirm('Are you sure you want to delete this record?') == true ) this.form.submit(); return true;"
In JavaScript, buttons are required to be generated by <INPUT TYPE=BUTTON OnClick="..."> tags.  If the DeleteButtonOnClick property is set, all the Delete buttons for the grid are generated using JavaScript.
The DeleteButtonOnClick property specifies the OnClick attribute for the buttons.  In the above sample code, confirmation from the user is handled by a JavaScript procedure which displays a confirmation window on clicking the delete button. The form is submitted only after the user clicks the OK button of the confirmation window.

Grid.UseImageButtons = False
Set this property to False for using the DeleteButtonOnClick property.

Rest of the lines are related to the validation of phone numbers. Server-side ASP scripting is used for the purpose.  A phone number is considered valid only if it contains exactly 10 numeric digits.  Non-numeric characters are irrelevant.

If Request("AspGridSave1") <> "" or Request("AspGridInsert1") <> "" Then
The above line checks for non-empty values in Request.("AspGridSave1") and Request.("AspGridInsert1"), respectively.  If the user has clicked the Save button (Edit mode) or Add button (Add New mode), then the variables will contain non-empty values.  The terminating number "1" in these variables corresponds to the NumberOnPage property. The NumberOnPage property is 1 by default.

Phone = Request("Field2")
The purpose of this line is to extract the value of the Phone field. "Field2" is the input variable name generated by iASP_Grid.  The field numbering is zero-based.  Field0 corresponds to ID, Field1 to name and Field2 to phone.  If NumberOnPage property is 2, the fields would be numbered as follows: Field101, Field102 and Field103. This numbering scheme avoids name collisions if there are more than one grid on the same page.

Count = 0
For i = 1 to Len(Phone)
   if Mid(Phone, i, 1) >= "0" and Mid(Phone, i, 1) <= "9" Then Count = Count + 1
Next

if Count <> 10 Then
   Response.Redirect "Sample5Error.asp"
End If

The above lines count numeric characters in the Phone string. The total should be 10 otherwise the browser is redirected to an error page. It is necessary to call Response.Redirect before calling Display.

If you require technical support please send complete details about the problem you are having to support@halcyonsoft.com.


Copyright © 1998-2002, Halcyon Software Inc. All rights reserved.