Dynamically generating JavaScript-generated form validation code with ASP

Source: Internet
Author: User
Tags client
javascript| Form Validation | Validation of dynamic forms is a common concern in developing Web applications. Sometimes we have to make sure that some items in the form must be filled in, must be numbers, must be a specified number of digits, and so on, we need to use the form validation, generally we commonly used form validation in 2 ways:

1, write JavaScript or VBScript form validation function, in the client to verify;
2, after the form submitted, using ASP method Request.Form get form input value to judge, and then return the result, this is in the service side to verify;

These 2 ways have their advantages and disadvantages, such as the 1th way faster, and usually use the warning box, users can quickly follow the prompts to complete the form, but the disadvantage is that the user's browser must support JavaScript script, or if he closed the JavaScript, then !@#$%&^* (Xiao Tian has fallen on the ground ^_^) while the 2nd approach is better compatible, the disadvantage is that the speed is slower (submitted to the server, returned) and is inconvenient to use. This is primarily a JavaScript method to verify, of course, if the simultaneous use of 2 ways to verify the most insurance, but (Khan ...). We are tired of the programmers:)

This explains the 2 ways in which form validation is made, and the following is the concept of dynamically generating JavaScript. Why do you want to generate it dynamically? Because this kind of client authentication code is very cumbersome, if every time to write their own really tired ah! Friends using the Dreamweaver or UD may often use form-validated Plug-ins, and the generated code is not artistically available, and many are not used (code redundancy). The field is about generating code that exactly matches the form.

Statement: Small field of JavaScript is not proficient, here just want to talk about dynamic generation of methods, JS Master can completely change their own.

So here we go.

1, let's take a look at a simple JavaScript validation code
<formname="form1"><inputtype="text"name="name"></form>

This code is the name form item that validates the form Form1 and must be filled in. Here are a few key parts: Form field name, table item name, judgment statement; These are the keys to writing ASP functions below.

2. How to generate JavaScript code. The simplest, is to use Response.Write output, such as the above code output can be



The vbCrLf Here is a carriage return line,& is a connector, &_ is the character of the code line-wrapping connection.

3, this code of the head and tail is basically fixed unchanged, the change is the middle of the if Judge part, we can first write this part of the function, the field has written a good one, you can refer to, the following is the code
Functionfindjs (FRMNAME,ERRSTR)
Dimtmparr
Dimi
' Parameter value
I=0
' Get the Error list, build the array
Tmparr=split (errstr, "|")
' Output query criteria
Selectcasetmparr (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&_
"Returnfalse;" &vbcrlf&_
"}" &vbcrlf
' "Else" &vbcrlf&_
' "Returntrue;" &vbcrlf
Exitfunction
Case "1" ' required type of Listmenu
Findjs= "If" (document. &frmName& "." &tmparr (i) & ". Value" = "" "" "" &vbcrlf&_
"{" &vbcrlf&_
"Window.alert ('" &tmparr (i+2) & ");" &vbcrlf&_
"Document." &frmName& "." &tmparr (i) & ". focus ();" &vbcrlf&_
"Returnfalse;" &vbcrlf&_
"}" &vbcrlf
' "Else" &vbcrlf&_
' "Returntrue;" &vbcrlf
Exitfunction
Case ' 2 ' must be a text type of 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&_
"Returnfalse;" &vbcrlf&_
"}" &vbcrlf
' "Else" &vbcrlf&_
' "Returntrue;" &vbcrlf
Exitfunction
Case "3" must be a text type of a 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&_
"Returnfalse;" &vbcrlf&_
"}" &vbcrlf
' "Else" &vbcrlf&_
' "Returntrue;" &vbcrlf
Exitfunction
Case ' 4 ' must be greater than 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&_
"Returnfalse;" &vbcrlf&_
"}" &vbcrlf
' "Else" &vbcrlf&_
' "Returntrue;" &vbcrlf
Exitfunction
Case "5" ' must be the text type of 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&_

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.