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! Get used to DW (Dreamweaver) Or UD friends may usually use Form Verification plug-ins, and the code generated after use 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">
<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 Condition
Select case tmparr (I + 1)
Case "0" ': required text type
Findjs = "If (document." & frmname & "." & tmparr (I) & ". Value) =") "& vbcrlf &_
"{" & Vbcrlf &_
"Window. Alert ('" & tmparr (I + 2) & "');" & vbcrlf &_
"Document." & frmname & "." & tmparr (I) & ". Select ();" & vbcrlf &_
"Document." & frmname & "." & tmparr (I) & ". Focus ();" & vbcrlf &_
"Return false;" & vbcrlf &_
"}" & Vbcrlf
'"Else" & vbcrlf &_
'"Return true;" & vbcrlf
Exit Function
Case "1" 'mandatory listmenu type
Findjs = "If (document." & frmname & "." & tmparr (I) & ". Value) =") "& vbcrlf &_
"{" & Vbcrlf &_
"Window. Alert ('" & tmparr (I + 2) & "');" & vbcrlf &_
"Document." & frmname & "." & tmparr (I) & ". Focus ();" & vbcrlf &_
"Return false;" & vbcrlf &_
"}" & Vbcrlf
'"Else" & vbcrlf &_
'"Return true;" & vbcrlf
Exit Function
Case "2" 'must be of the text type of a number
Findjs = "If (isnan (document." & frmname & "." & tmparr (I) & ". Value)" & vbcrlf &_
"{" & Vbcrlf &_
"Window. Alert ('" & tmparr (I + 2) & "');" & vbcrlf &_
"Document." & frmname & "." & tmparr (I) & ". Select ();" & vbcrlf &_
"Document." & frmname & "." & tmparr (I) & ". Focus ();" & vbcrlf &_
"Return false;" & vbcrlf &_
"}" & Vbcrlf
'"Else" & vbcrlf &_
'"Return true;" & vbcrlf
Exit Function
Case "3" 'must be of the text type of the specified number of digits
Findjs = "If (document. "& frmname &". "& tmparr (I )&". value. length = "& tmparr (I + 3) &") "& vbcrlf &_
"{" & Vbcrlf &_
"Window. Alert ('" & tmparr (I + 2) & "');" & vbcrlf &_
"Document." & frmname & "." & tmparr (I) & ". Select ();" & vbcrlf &_
"Document." & frmname & "." & tmparr (I) & ". Focus ();" & vbcrlf &_
"Return false;" & vbcrlf &_
"}" & Vbcrlf
'"Else" & vbcrlf &_
'"Return true;" & vbcrlf
Exit Function
Case "4" 'must be of the text type greater than the specified number of digits
Findjs = "If (document. "& frmname &". "& tmparr (I )&". value. length <"& tmparr (I + 3) &") "& vbcrlf &_
"{" & Vbcrlf &_
"Window. Alert ('" & tmparr (I + 2) & "');" & vbcrlf &_
"Document." & frmname & "." & tmparr (I) & ". Select ();" & vbcrlf &_
"Document." & frmname & "." & tmparr (I) & ". Focus ();" & vbcrlf &_
"Return false;" & vbcrlf &_
"}" & Vbcrlf
'"Else" & vbcrlf &_
'"Return true;" & vbcrlf
Exit Function
Case "5" 'must be the text type of the email
Findjs = "If ((! Emailreg. test (document. "& frmname &". "& tmparr (I )&". value) & (document. "& frmname &". "& tmparr (I )&". value! = ') "& Vbcrlf &_
"{" & Vbcrlf &_
"Window. Alert ('" & tmparr (I + 2) & "');" & vbcrlf &_
"Document." & frmname & "." & tmparr (I) & ". Select ();" & vbcrlf &_
"Document." & frmname & "." & tmparr (I) & ". Focus ();" & vbcrlf &_
"Return false;" & vbcrlf &_
"}" & Vbcrlf
'"Else" & vbcrlf &_
'"Return true;" & vbcrlf
Exit Function
Case "6" 'must be a-Z or 0-9 text
Findjs = "If ((! Pwdreg. test (document. "& frmname &". "& tmparr (I )&". value) & (document. "& frmname &". "& tmparr (I )&". value! = ') "& Vbcrlf &_
"{" & Vbcrlf &_
"Window. Alert ('" & tmparr (I + 2) & "');" & vbcrlf &_
"Document." & frmname & "." & tmparr (I) & ". Select ();" & vbcrlf &_
"Document." & frmname & "." & tmparr (I) & ". Focus ();" & vbcrlf &_
"Return false;" & vbcrlf &_
"}" & Vbcrlf
'"Else" & vbcrlf &_
'"Return true;" & vbcrlf
Exit Function
Case "7" 'confirm that the password and password must be of the same text type
Findjs = "If (document." & frmname & "." & tmparr (I) & ". Value )! = (Document. "& frmname &". "& tmparr (I + 3) &". Value) "& vbcrlf &_
"{" & Vbcrlf &_
"Window. Alert ('" & tmparr (I + 2) & "');" & vbcrlf &_
"Document." & frmname & "." & tmparr (I) & ". Select ();" & vbcrlf &_
"Document." & frmname & "." & tmparr (I) & ". Focus ();" & vbcrlf &_
"Return false;" & vbcrlf &_
"}" & Vbcrlf
'"Else" & vbcrlf &_
'"Return true;" & vbcrlf
Exit Function
End select
End Function
The frmname parameter indicates the name of the form field, and errstr is an array of form items, judgment types, and error prompts. Its syntax is as follows:
"Form item name 1 | judgment type 1 | error prompt 1 | [optional parameter 1], form item name 2 | judgment type 2 | error prompt 2 | [optional parameter 2],..."
Form Item Name: such as name, which is customized
Judgment type: 0, 1, 2, 3, and so on in the case statement.
Error message: for example, the name must be filled in. The error message is customized.
Optional parameters: for example, when determining whether the password and password must be of the same text type, the optional parameter is used to determine the name of a password form item; when the password must be of the specified number of digits of the text type, the optional parameter is the specified number of digits. Of course you can select several parameters, depending on how the if statement is compiled.
We have listed eight if judgment statements for different situations. You can add them again. Pay attention to some special sections, such as the 5email format (emailreg. test), here the regular expression is used, and the definition of the regular expression can be put out of the IF judgment.
4. Function checkform_js (frmname, errstr ). This function is used to finally integrate JavaScript if judgments. The Code is as follows:
Sub checkform_js (frmname, errstr)
Dim tmparr
Dim I
Dim strshow 'output JS string
'Get the Error List and create an array
Tmparr = Split (errstr ,",")
'Write JS
For I = 0 to ubound (tmparr)
If I <> 0 then
Strshow = strshow & "else" & findjs (frmname, tmparr (I ))
Else
Strshow = strshow & findjs (frmname, tmparr (I ))
End if
Next
'Output
Strshow = "<script language = JavaScript>" & vbcrlf &_
"<! -- "& Vbcrlf &_
"// Powers by Xiaotian 2002" & vbcrlf &_
"Function checksubmit ()" & vbcrlf &_
"{" & Vbcrlf &_
"Var emailreg =/^ [_ a-z0-9] + @ ([_ a-z0-9] +/.) + [a-z0-9] {2, 3} $/;" & vbcrlf &_
"Var pwdreg =/[a-z0-9] $/;" & vbcrlf &_
Strshow &_
"Else" & vbcrlf &_
"Return true;" & vbcrlf &_
"}" & Vbcrlf &_
"// -->" & Vbcrlf &_
"</SCRIPT>"
Response. Write strshow
End sub
You have noticed the following section:
If I <> 0 then
Strshow = strshow & "else" & findjs (frmname, tmparr (I ))
Else
Strshow = strshow & findjs (frmname, tmparr (I ))
End if
The function is to write the 1st JavaScript if statements later as else if, while
"Var emailreg =/^ [_ a-z0-9] + @ ([_ a-z0-9] +/.) + [a-z0-9] {2, 3} $/;" & vbcrlf &_
"Var pwdreg =/[a-z0-9] $/;" & vbcrlf &_
The Section code is the definition of the regular expression. You can expand it as needed.
5. usage. We can write these two functions in a file, such as checkform_js.asp, and then call them on the page to be used, for example:
<! -- # Include file = "checkform_js.asp" -->
<%
Call checkform_js ("frm", "Name | 0 | Name required, number | 2 | number must be a number, number | 3 | number specified as 6-digit | 6, email | 5 | Incorrect email format ")
%>
<Form name = "frm">
Name: <input type = "text" name = "name">
No.: <input type = "text" name = "Number">
Email: <input type = "text" name = "email">
<Input type = "Submit" name = "Submit" value = "Submit">
</Form>
In actual use, errstr may be a long string. In writing, we can use a line break method. For example, we can write the preceding errstr as follows:
<%
Dim errstr
Errstr = "name | 0 | Name required ,"&_
"Number | 2 | number must be a number, number | 3 | number is specified as 6 digits | 6 ,"&_
"Email | 5 | Incorrect email format"
Call checkform_js ("frm", errstr)
%>
Note: Because the delimiter of the array in the function is "|" and ",", the error message in errstr cannot use these two characters. You can use a full-width replacement.
6. Code running status. After the above Code is run, you can get the following javascript:
<Script language = JavaScript>
<! --
// Power by Xiaotian 2002
Function checksubmit ()
{
VaR emailreg =/^ [_ a-z0-9] + @ ([_ a-z0-9] +/.) + [a-z0-9] {2, 3} $ /;
VaR pwdreg =/[_ a-z0-9] $ /;
If (document. frm. Name. Value) = "")
{
Window. Alert ('name required ');
Document. frm. Name. Select ();
Document. frm. Name. Focus ();
Return false;
}
Else if (isnan (document. frm. Number. Value ))
{
Window. Alert ('Number must be number ');
Document. frm. Number. Select ();
Document. frm. Number. Focus ();
Return false;
}
Else if (document. frm. Number. value. Length = 6)
{
Window. Alert ('Number specified as 6 bs ');
Document. frm. Number. Select ();
Document. frm. Number. Focus ();
Return false;
}
Else if ((! Emailreg. Test (document. frm. Email. Value) & (document. frm. Email. value! = '))
{
Window. Alert ('incorrect email format ');
Document. frm. Email. Select ();
Document. frm. Email. Focus ();
Return false;
}
Else
Return true;
}
// -->
</SCRIPT>
If you are familiar with JS, you can expand this function by yourself. I believe this function can meet your verification requirements.