Form Verification is a common issue in WEB application development. Sometimes we must ensure that some 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, there are two common form verification methods:
1. Compile the form verification function of JavaScript or VBScript and verify it on the client;
2. After the Form is submitted, use the ASP Method Request. Form to obtain the input value of the Form for judgment, and then return the result, which is verified on the server;
Both methods have their own advantages and disadvantages. For example, the 1st methods are fast and usually use the warning box method. You can quickly complete the form filling as prompted, however, the disadvantage is that your browser must support JavaScript scripts. Otherwise, if JavaScript is disabled! @ # $ % & ^ * (ODA has fallen to the ground ^_^). The compatibility of the 2nd methods is good, but the disadvantage is that the speed is slow (submitted to the server and returned) it is also inconvenient to use. This is mainly verified using JavaScript. Of course, it is the most safe to verify using two methods at the same time, but (Khan ............) It's exhausting our programmers :)
The above explains the method in Form Verification 2. The following describes the concept of dynamic JavaScript generation. Why dynamic generation? This is because the verification code of the client is very cumbersome. If you have to write your own code every time, it is really tiring! Friends who are familiar with DW (Dreamweaver) or UD may usually use Form Verification plug-ins, and the generated code is not artistic, and many cannot be used (Code redundancy ). What Mr. Oda wants to talk about is to generate code that fully complies with the form.
Statement: Mr. Oda is not proficient in JavaScript. Here I just want to talk about the dynamic generation method. The JS experts can modify it by themselves.
So let's get started.
1. Let's take a look at a simple JavaScript verification code:
<Script language = javascript>
<! --
// Power by xiaotian 2002
Function checkSubmit ()
{
If (document. form1.name. value) =)
{
Window. alert (name required );
Document. form1.name. select ();
Document. form1.name. focus ();
Return false;
}
Else
Return true;
}
// -->
</Script>
<Form name = "form1" onsubmit = "javascript: return checkSubmit ()">
<Input type = "text" name = "name">
</Form>
This code is the name form entry of the verification form form1, which must be filled in. Here, there are several key parts: Form domain name, form item name, and judgment statement. These are the keys for compiling ASP functions below.
2. How to generate JavaScript code. The simplest is to use Response. Write to output the Code. For example, output the code above can be:
<%
Response. Write "<script language = javascript>" & vbCrlf &_
"<! -- "& VbCrlf &_
"// Powers by xiaotian 2002" & vbCrlf &_
"Function checkSubmit ()" & vbCrlf &_
"{" & VbCrlf &_
"If (document. form1.name. value) =)" & vbCrlf &_
"{" & VbCrlf &_
"Window. alert (name required);" & vbCrlf &_
"Document. form1.name. select ();" & vbCrlf &_
"Document. form1.name. focus ();" & vbCrlf &_
"Return false;" & vbCrlf &_
"Else" & vbCrlf &_
"Return true;" & vbCrlf &_
"}" & VbCrlf &_
"// -->" & VbCrlf &_
"</Script>" & vbCrlf &_
%>
VbCrlf is a carriage return line break, a connector, and a newline connection character.
3. the header and tail of this Code are basically fixed, and the change is the if judgment part in the middle. We can write this part as a function first, and ODA has already written it, you can refer to the following 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 &_