ASP dynamically generated JavaScript form validation code

Source: Internet
Author: User
Tags format array exit end html tags include split window
javascript| Form Validation | dynamic Online down an ASP dynamic generated form to verify JavaScript code, the expansion of their own. I have forgotten the name of the author, but I remember clearly that the code from the first to the Nineth is the author's original one. As for the nine to the 15th is my own add oh. And because of my limited level, from Nineth to 15th will not be effective AH? Oh, in fact, it is not necessary to paste it here, because this small will trick who will use, I put it's role is: development can not find the function when the copy here. Oh, because I don't have a burner or a hard drive. There is no way to carry these codes around, but fortunately, the Internet can be everywhere in Beijing. So put this here, use the time CTRL + C, and then CTRL + V OK.
[Copy this Code] code:<%
'****************************************************************
' Function name: Checkform_js (FRMNAME,ERRSTR)
' Function: Dynamically write JavaScript Form validation functions using ASP Checksubmit ()
' Use method: 1, <!--Include file=url+ the page >; of this function
' 2, <form >;
' The original author has been forgotten, two times development author: guo.q.m
' Last update: 2004
'****************************************************************
Help
'---------------------------------------------------------------
' • Parameter description:
' Frmname: Name of form Field
' ERRSTR: Verify the list, such as: "num|3| model must be no less than 8 bits |8,email|5| Please enter the correct email format", here
' num indicates the form domain name, 3 is the validation parameter, and 8 is the number of digits not less than (optional)
'
' • Validate parameter list:
' 0: Required text type
' 1: Required type of Listmenu
' 2: Text type that must be a number
' 3: Text type that must be a specified number of digits
' 4: Text type that must be greater than the specified number of digits
' 5: Must be the text type of email
' 6: The text type of the character that must be a-Z or 0-9
' 7: Confirm password and password must be equal to the text type
' 8: Confirm that the text type does not begin with a number
' 9: Text type of 10-888888 format must be included
' 10: Must not contain Chinese, spaces and other illegal characters of the text type, that can only contain "_" "-" "0-9" "A-Z" A-Z "
' 11: The text type must contain only numbers and '-'
' 12: The text type must be the correct URL
' 13: Text type that must be less than the specified number of digits
' 14: Text type with HTML tags must not be included
' 15: Confirm that the Select type that is not disabled must choose a format: Check the table item |15| Message | Association item "
' NOTE: If you have a cascading menu, put the validation process for the cascading menu to the final Test!!!!
'------------------------------------------------------------------------
%>
<%
Sub Checkform_js (FRMNAME,ERRSTR)
Dim Tmparr
Dim I
Dim strshow ' Output JS string
' Get the Error list, build the 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&_
"//power by Guoquanman" &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&_
"var uidbeginreg =/^[0-9]+[_a-z0-9]/;" &vbcrlf&_
"var phonereg =/\d{2}-\d{5}/;" &vbcrlf&_
"var phonedetailreg =/[^0-9\-]/;" &vbcrlf&_
"var uidreg =/[^a-za-z0-9_\-]/;" &vbcrlf&_
"var Htmlreg =/< (. *) >.*<\/\1>/;" &vbcrlf&_
"var re1 =/^http:\/\/[a-za-z][a-za-z0-9\-]*[a-za-z]*\./;" &vbcrlf&_
"var re2 =/^http:\/\/[0-9]{1,5}[a-za-z]*[0-9]*\./;" &vbcrlf&_
"var Re3 =/\. {2,}/; " &vbcrlf&_
"var Re4 =/\:{2,}/;" &vbcrlf&_
"var Re5 =/\/{3,}/;" &vbcrlf&_
"var re6 =/\,+/;" &vbcrlf&_
"var re7 =/\!+/;" &vbcrlf&_
"var Re8 =/\@+/;" &vbcrlf&_
"var re9 =/\#+/;" &vbcrlf&_
"var re10 =/\$+/;" &vbcrlf&_
"var re11 =/\^+/;" &vbcrlf&_
"var re12 =/\*+/;" &vbcrlf&_
"var re13 =/\|+/;" &vbcrlf&_
"var re14 =/\. [a-z0-9_&=?\/]*[a-za-z0-9\/\~]{2,}$/; " &vbcrlf&_
Strshow&_
"Else" &vbcrlf&_
"Return true;" &vbcrlf&_
"}" &vbcrlf&_
"//-->" &vbcrlf&_
"</script>"
Response.Write Strshow
End Sub
Function Findjs (FRMNAME,ERRSTR)
Dim Tmparr
Dim I
' Parameter value
I=0
' Get the Error list, build the array
Tmparr=split (errstr, "|")
' Output query criteria
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
Exit Function
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&_
"Return false;" &vbcrlf&_
"}" &vbcrlf
Exit Function
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&_
"Return false;" &vbcrlf&_
"}" &vbcrlf
Exit Function
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&_
"Return false;" &vbcrlf&_
"}" &vbcrlf
Exit Function
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&_
"Return false;" &vbcrlf&_
"}" &vbcrlf
Exit Function
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&_
"Return false;" &vbcrlf&_
"}" &vbcrlf
Exit Function
Case "6" ' must be A-Z or 0-9-character text type
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
Exit Function
Case "7" ' Confirm password and password must be equal to 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
Exit Function
Case "8" confirms the text type that starts with a number
Findjs= "If" (Uidbeginreg.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
Exit Function
Case "9" ' confirmation of phone number in 10-101212 format
Findjs= "If" (!phonereg.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
Exit Function
Case "10" is confirmed to contain only the English alphabet and the text "-", "_". (i.e. excluding Chinese and other special characters)
Findjs= "If" (Uidreg.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
Exit Function
Case "11" confirms only the number and the "-" text type (phone number and fax used)
Findjs= "If" (Phonedetailreg.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
Exit Function
Case "12" to confirm whether it is a valid URL!
Findjs= "If" ((!re1.test (document.) &frmName& "." &tmparr (i) & ". Value") && (!re2.test (document.) &frmName& "." &tmparr (i) & ". Value)" &_
"&& (document.) &frmName& "." &tmparr (i) & ". value!=") | | &_
"(Re3.test (document.) &frmName& "." &tmparr (i) & ". Value)" | | (Re4.test (document.) &frmName& "." &tmparr (i) & ". Value)" | | " &_
"(Re5.test (document.) &frmName& "." &tmparr (i) & ". Value)" | | (Re6.test (document.) &frmName& "." &tmparr (i) & ". Value)" | | " &_
"(Re7.test (document.) &frmName& "." &tmparr (i) & ". Value)" | | (Re8.test (document.) &frmName& "." &tmparr (i) & ". Value)" | | " &_
"(Re9.test (document.) &frmName& "." &tmparr (i) & ". Value)" | | (Re10.test (document.) &frmName& "." &tmparr (i) & ". Value)" | | " &_
"(Re11.test (document.) &frmName& "." &tmparr (i) & ". Value)" | | (Re12.test (document.) &frmName& "." &tmparr (i) & ". Value)" | | " &_
"(Re13.test (document.) &frmName& "." &tmparr (i) & ". Value)" | | (!re14.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
Exit Function
Case "13" confirms the text type that is not greater than the fixed 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
Exit Function
Case "14" confirms the text type containing HTML tags
Findjs= "If" (Htmlreg.test (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
Exit Function
Case "15"
'==========================================================
' Confirm that the Select type that is not disabled must choose a format: state|15| Please select the name of the province!|selcountry|city| please select city information
' Note: Cascading menu item 1th when selectedindex!=0, item 2nd 3rd is disabled! No need to check the other two
' When the cascading menu item 1th selectedindex==0, the 2nd item SelectedIndex cannot be 0, when the second item selectedindex!=0, the 3rd item SelectedIndex also cannot be 0
' This is used to check the country/province/City three cascading menu, when the country is not for China, provinces and cities can not fill, when China, must fill in the province and the relative city!
Findjs= "if (document.") &frmName& "." &tmparr (i+3) & ". SelectedIndex ==0)" &vbcrlf&_
"{" &vbcrlf&_
"If (document.") &frmName& "." &tmparr (i) & ". SelectedIndex ==0)" &vbcrlf&_
"{" &vbcrlf&_
"Window.alert ('" &tmparr (i+2) & ");" &vbcrlf&_
"Document." &frmName& "." &tmparr (i) & ". Focus;" &vbcrlf&_
"Return false;" &vbcrlf&_
"}" &vbcrlf&_
"Else if (document.") &frmName& "." &tmparr (i) & ". SelectedIndex!= 0)" &vbcrlf&_
"{" &vbcrlf&_
"If (document.") &frmName& "." &tmparr (i+4) & ". SelectedIndex = 0)" &vbcrlf&_
"{" &vbcrlf&_
"Window.alert ('" &tmparr (i+5) & ");" &vbcrlf&_
"Document." &frmName& "." &tmparr (i+4) & ". Focus;" &vbcrlf&_
"Return false;" &vbcrlf&_
"}" &vbcrlf&_
"}" &vbcrlf&_
"}" &vbcrlf
Exit Function
Case "16" to confirm that the select type that is not disabled must choose a format: Check table single |16| hint | association item "NOTE: When the associated item is the first item, confirm the start!
Findjs= "if (document.") &frmName& "." &tmparr (i+3) & ". SelectedIndex!= 0)" &vbcrlf&_
"{" &vbcrlf&_
"If (document.") &frmName& "." &tmparr (i) & ". SelectedIndex = 0)" &vbcrlf&_
"{" &vbcrlf&_
"Window.alert ('" &tmparr (i+2) & ");" &vbcrlf&_
"Document." &frmName& "." &tmparr (i) & ". Focus;" &vbcrlf&_
"Return false;" &vbcrlf&_
"}" &vbcrlf&_
"}" &vbcrlf
Exit Function
End Select
End Function
%>
***********************************************************************
Use Example:
[Copy this Code] code:<% @LANGUAGE = "VBSCRIPT" codepage= "936"%>
<!--#Include file = "inc/check_formjs.asp"-->
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<%
'==========================================================
' Form Validation instance
'==========================================================
' 1, <!--#Include file = "inc/check_formjs.asp"-->;
' 2, Draw form: name= "" onsubmit = "Javascript:return Checksubmit ()" attention to case;
' 3, the definition of guo_error//A sentence can only appear a "," such as the number of digits and confirm the password must add "|" Specify parameters;
' 4, call Checkform_js ("Formname,guo_error")
' Www.china-u.com
'==========================================================
Dim Guo_error
Guo_error = "text|0| text item must be filled in!,"
Guo_error = guo_error & "number|0| numeric entries must be filled in and must be numbers!" ,"
Guo_error = guo_error & "number|2| numeric entries must be numbers!,"
Guo_error = guo_error & digital|3| must be 6 bits!| 6, "
Guo_error = guo_error & moredigital|4| Multiple-bit items must be greater than 4-bit!| 4, "
Guo_error = guo_error & "email|5| Mail entry must be filled in email address!, "
Guo_error = guo_error & "caractor|6| character entries must be 0-9a-z characters!,"
Guo_error = guo_error & password2|7| Confirm that the password must be consistent with the password entry!|password1, "
Guo_error = guo_error & "listmenu|1| must choose!"
Guo_error = guo_error & "uid|8| User name cannot begin with a number!, "
Call Checkform_js ("Form1", Guo_error)
'==========================================================
' Form verification process
'==========================================================
' 1, through split (Guo_error, ".") Split to Array Temparr ();
' 2, through split (Temparr, "|") Split to Array Temparr ();
' 3, Select case split (Temparr (i+1), "|") Perform validation and output error message split (Temparr (i+2));
' 4, JavaScript is case-sensitive, so the name of each form component should pay attention to write consistent yo ~ ~
'==========================================================
%>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title> Form Validation Instance </title>
<body>
<form name= "Form1" method= "Post" action= "check_form.asp" >
<p align= "center" > The form to be validated </p>
***************************************************************
Most of the above code has passed the test, but is not all good, I can only say that cascading drop-down menu is not very useful. See which one or the web girl to improve it. Oh

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.