Let's open the file Sample5.asp.

Let us also try and enter a phone number with the number of digits less or greater than 10. When trying to save the record, we will be redirected to the following error page:
This validation was achieved through server-side ASP script.
| <%
Set Grid = Server.CreateObject("Persits.AspGrid.1") Grid.FileName = "Sample5.asp" Grid.Connect "AspGrid", "", "" 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
<HTML>
<% Grid.Display %>
</BODY>
|
Let's now examine the code.
Grid.DeleteButtonOnClick
= "if( confirm('Are you sure you want to delete this record?') == true
) this.form.submit(); return true;"
Grid.UseImageButtons
= False
To properly activate JavaScript, we need to use buttons generated by <INPUT TYPE=BUTTON OnClick="..."> for our Delete buttons. The DeleteButtonOnClick property set on the second line specifies the OnClick attribute for all the Delete buttons in the grid. In this sample, it is a JavaScript procedure which asks for a confirmation from the user and submits the form if the confirmation is received.
You must always set UseImageButtons to False if you are using the DeleteButtonOnClick property.
The next lines perform the validation of phone numbers using server-side ASP script. For the sake of discussion, we consider a phone number to be valid if it contains exactly 10 digits. The amount of non-numeric characters in irrelevant.
If Request("AspGridSave1") <> "" or Request("AspGridInsert1") <> "" Then
First of all, we check if we the user clicked the Save button (if in the Edit mode) or Add button (if in the Add New mode). If this is the case, the variables Request.QueryString("AspGridSave1") and Request.QueryString("AspGridInsert1"), respectively, will contain non-empty values. The terminating number "1" in these variables corresponds to the NumberOnPage property (which is 1 by default).
Phone = Request("Field2")
We then extract the new value for the Phone field. "Field2" is one of the input variable names generated by AspGrid. Notice that the field numbering is zero-based, so Field0 would correspond to ID, Field1 to name and Field2 to phone. Remember also that if NumberOnPage is, say, 2, the fields would be numbered as follows: Field101, Field102 and Field103. This complex numbering scheme is necessary to avoid name collisions if several grids are placed on one 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 remaining part if fairly trivial. We count numeric characters in
the Phone string and if the total is not 10 we redirect the browser to
an error page. It is important to call Response.Redirect before
calling Display or it will be too late - the new values will already
be saved in the database.