Home >php教程 >PHP开发 >Use ASP to dynamically generate JavaScript form verification code

Use ASP to dynamically generate JavaScript form verification code

高洛峰
高洛峰Original
2016-11-25 09:53:281173browse

Form validation is a level often encountered in developing WEB applications. Sometimes we have to ensure that certain items in the form must be filled in, must be numbers, must be a specified number of digits, etc. At this time, we need to use form verification. Generally, we commonly use two methods of form verification:
1. Write a JavaScript or VBScript form verification function to verify on the client side;
2. After the form is submitted, use the ASP method Request.Form to get the input value of the form for judgment, and then return the result. This is verified on the server side;

These two methods have their advantages and disadvantages. For example, the first method is faster and usually uses an alert box. The user can quickly complete the form filling in according to the prompts. However, the disadvantage is that the user's browser must be Supports JavaScript scripts, otherwise if he turns off JavaScript, then!@#$%&^* (Oda has fallen to the ground^_^); while the second method has better compatibility, but the disadvantage is speed It is relatively slow (submitted to the server and returned) and inconvenient to use. This time I mainly use JavaScript to verify. Of course, it is safest to use two methods to verify at the same time, but (sweat...) it will make us programmers exhausted:)

 The form is explained above For the two methods of verification, let’s talk about the concept of dynamically generating JavaScript in detail. Why do we need to generate it dynamically? Because this kind of client-side verification code is very cumbersome, it would be really tiring if you have to write it yourself every time! Friends who are used to DW (Dreamweaver) or UD may usually use form validation plug-ins. The code generated after use is not artistic, and many of them are unused (code redundancy). What Oda wants to talk about is to generate code that fully conforms to the situation of the form.

Disclaimer: Xiaotian is not proficient in JavaScript. I just want to talk about the dynamic generation method. JS masters can modify it themselves.

 Then let’s get started.

1. Let’s first take a look at a simple JavaScript verification code:







This code is the name form item of the verification form form1, and the content must be filled in. Here are several key parts: form field names, form item names, and judgment statements; these are the keys to our ASP function writing below.

2. How to generate JavaScript code. The simplest is to use Response.Write to output. For example, the above code output can be:

<%
Response.Write ""&vbCrlf&_
%>

The vbCrlf here is a carriage return and line feed,& is the connector, & _ is the character for code newline concatenation.

3. The head and tail of this code are basically fixed. What changes is the if judgment part in the middle. We can write this part as a function first. Oda has already written one. You can refer to it. The following is Code:

Function findJS(frmName,errStr)
Dim tmpArr
Dim i
Parameter value
i=0
Get the error list and create an array
tmpArr=Split(errStr,"|")
Output query conditions
Select Case tmpArr (i+1)
Case "0" Required Text type
findJS="if ((document."&frmName&"."&tmpArr(i)&".value)=="""")"&vbCrlf&_ Please note when reprinting Source: Use ASP to dynamically generate JavaScript form verification code

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn