The generation and validation of JavaScript processing complex forms in ASP 1th/3 page _ Application Tips

Source: Internet
Author: User
Tags data structures dsn odbc numeric value access database
A complex form here is a form that contains many different types of input, such as a drop-down list box, a single line of text, multi-line text, numeric values, and so on. There is a need to have a dynamic build program for a form when you often need to replace such forms. This article is about a system that uses a database to save forms to define data, to dynamically generate form HTML code with ASP scripts, and to validate form input scripts.
Definition of database table structure
Forms such as weekly surveys are often visible on the web, which is a form that needs to be updated frequently. If you have a program that dynamically generates forms and their validation scripts, you can significantly reduce the amount of work that you do to make these forms.
In the example of dynamic form generation and validation in this article, we use an Access database to store the definition information about the form, and for simplicity, the data that the user enters into the form is saved to the same database. It takes two tables to define a form: The first table (definitons) is used for the definition of the form input field, and the second table (Lists) Saves additional information about each input field, such as selecting a list.
The table definitons contains the following fields:
fieldname--the variable name that gives the form input field
label--is a text label that shows the prompt text in front of the input field
type--A single character that represents the form input field and the type of the input value
Specifically as follows:
(t) text input box, that is, < input type= "text" >.
(n) Text entry box, but requires a numeric value to be entered.
(m) Memo-type content, for comments or other large amounts of text input, which is a multiple-line edit box.
(b) Require the importation of "yes" or "no". A check box is used in this implementation to get this input, and the text label for the check box is yes. If the user selects it, the return value is "on".
(r) radio button.
(l) Drop-down list box.
min--is valid only for numeric input values, where the minimum value is given. In this example, there is an age-numeric input box with a minimum value of 1.
max--The value of this field is related to the form of the input field. For a numeric input box, it represents the maximum allowable value. For example, the max value of "age" is 100. For a text entry box, Max represents the maximum number of characters allowed. For a multiline edit box, Max represents the number of lines of text in the visible area.
required--indicates whether it must be entered. If the value of this type is not entered, the input validator will report an error. In the form, the value that must be entered is marked with an asterisk and prompts the user with a footnote that the class value must be entered.
The sample form in this article is an ASP programmer questionnaire, which is defined in the Definitons table as follows:
FieldName Label Type Min Max Required
Name text (t)-50 No
Age number (n) 1 100 No
Sex Sex radio button (r)--Yes
e-mail address text (t)--IS
Language programming language Drop-down list box (l)--No
Table lists is used to save some additional information about the input field definition, in this case there are "Sex" and "Languages" two input values to use. The table lists is very simple and contains only the following three fields:
fieldname--which form input field the current record belongs to
value--the value of the selected item
label--the prompt text for the selected item that the user sees
The input field "Sex" can only be selected from two values: "Male" or "female". "Language" lists several programming languages that can be applied to the ASP environment, including: Vbscript,javascript,c,perl and "other".
The third table, "Records," saves the user-submitted content, and it also contains three fields, each of which corresponds to a user's commit:
record--memo type, user input saved as a query string.
created--the date and time when the user submitted the form. remoteip--the IP address of the form submitter.
In the actual application may want to gather more information about the user, for the simple account, this example only records the submission time and the user IP address these two additional information.
Ii. preparatory work
Once you have finished defining the data structures and forms, you can then write the script. The task of the script is to generate the form and process the user-submitted form.
Whether the form is generated or processed, the following three processes (tasks) are essential: the first is to determine the type of validation, the validation type values are obtained from the query string when the form is generated, and the hidden fields are read from the form when the form is processed. There are four types of forms authentication that the program supports: No authentication, client JavaScript validation, server-side ASP script validation, client and server-side validation (code-named 0 to 3 respectively). If you do not specify a valid authentication method in the query string, the fourth method of authentication is default. This validation process allows us to flexibly apply this form generation and processing system that can be performed only on the server side when the client prohibits JavaScript validation. The following is the code that determines the type of validation:
Check validation type
The following is a reference fragment:
Ivaltype = Request.QueryString ("Val")
If IsNumeric (ivaltype) = False Then Ivaltype = 3
If ivaltype > 3 or ivaltype < 0 Then Ivaltype =3
The second task is to open the database connection and create two Recordset objects: Rs Object, which is the main recordset object in this program to manipulate the definitions table; Rslist object that is used primarily to read data from the lists table. The sample program provides two types of database connection methods: Use ODBC DSN or do not use ODBC DSN (you need to create a DSN named dynamic when you use DSN, the code that uses the DSN connection database has been commented out).
The third task is to output some static HTML code, such as < head >In addition to the code that completes the above task, the remaining ASP scripts in the example application might generate two types of pages: A question form (see above) and a result page that appears after the form is submitted (the latter is also responsible for the user's record of submitting results). The easiest way to determine which part of the script to run is to check whether the form has been submitted: If so, the form is processed;
Generate a form or process a form?

If Len (Request.Form) = 0 Then
' Generate a form
... Slightly...
Else
' Process a form
... Slightly...
End If
current 1/3 page   1 23 Next read the full text
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.