Reprint: phpsoho Common Client form verification function Revision Main content: I. Usage introduction: Ii. Type Definition: 3. program files: History: 2005.2.25 Change to common client form verification function Revision 2004.12.21 1. modified the Integer Range verification; 2. Email verification has been modified to adapt to the situation where there is a dot in the account; 2004.12.20 1. Collect the prototype of this article from the Internet; Common form verification functions-available version Last modified: 2004.12.21 I. Usage introduction: The form verification function is stored in the functions. js file, which contains the script file in the webpage file you need to verify. The general syntax is: <SCRIPT type = "text/JavaScript" src = "./functions. js"> If the focus is not verified, bind the corresponding verification function to the onblur event of the Form Control. The usage is as follows: Integer checknumber () Floating Point checknumber () String checkstring () Date checkdate () Email checkemail () Example onblur = "checknumber ()" For Form submission and verification, make a judgment before the form is submitted. The usage is as follows: If (checkform ('form name ')) { Form name. Submit (); Return true; } Else { Return false; } You can also bind an onsubmit event as follows: Onsubmit = "Return checkform ('form name ')" Ii. Type Definition: 1. INTEGER (INT) Definition: Valuetype = "int" Attribute: Objname Object Name (string) Mustinput (true/false) Mininput minimum value (number) Maxinput maximum (number) Example: <Input type = "text" name = "test" valuetype = "int" objname = "total load capacity tons" mustinput = "true" maxinput = "10000"> 2. Float) Definition: Valuetype = "float" Attribute: Objname Object Name (string) Mustinput (true/false) Mininput minimum value (number) Maxinput maximum (number) Decimallen decimal places (number) Example: <Input type = "text" name = "test" valuetype = "float" objname = "" mustinput = "true" maxinput = "10000.50" decimallen = "2"> 3. String) Definition: Valuetype = "string" Attribute: Objname Object Name (string) Mustinput (true/false) Stringlen String Length (number) Example: <Input type = "text" name = "test" valuetype = "string" objname = "English ship name" mustinput = "true" stringlen = "100"> 4. Date) Definition: Valuetype = "date" Attribute: Objname Object Name (string) Mustinput (true/false) Example: <Input type = "text" name = "test" valuetype = "date" objname = "Start Date" mustinput = "true"> Note: The date can only be verified in the format of yyyy-mm-dd) 5. Email) Definition: Valuetype = "email" Attribute: Objname Object Name (string) Mustinput (true/false) Example: <Input type = "text" name = "test" valuetype = "email" objname = "email" mustinput = "true"> 6. Single-choice (radio) debugging is not successful yet Definition: Valuetype = "radio" Attribute: Objname Object Name (string) Mustselect mandatory (true/false) Example: <Input type = "radio" name = "test" valuetype = "radio" objname = "chartering method" mustselect = "true"> Note: For single-choice buttons in the same group, you only need to define the first one. 7. Check box has not been debugged yet Definition: Valuetype = "checkbox" Attribute: Objname Object Name (string) Min select (number) Maxselect maximum number (number) Example: <Input type = "checkbox" name = "test" valuetype = "checkbox" objname = "hobby" minselect = "2" maxselect = "5"> Note: You only need to define the first check button for the same group. 8. drop-down list box (select) Definition: Valuetype = "select" Attribute: Objname Object Name (string) Mustselect mandatory (true/false) Example 1: <Select name = "test" valuetype = "select" objname = "chartering method" mustselect = "true"> Example 2: <Select name = "test" valuetype = "select" objname = "chartering method" mustselect = "true"> <Option type = "checkbox" name = "Test2"> select <option> <Option type = "checkbox" name = "Test2"> 3 <option> <Option type = "checkbox" name = "Test2"> 4 <option> </SELECT> 9. List) Definition: Valuetype = "list" Attribute: Objname Object Name (string) Min select (number) Maxselect maximum number (number) Example: <Select name = "test" valuetype = "list" objname = "hobby" size = 5 minselect = "2" maxselect = "5"> /****************** Functions. JS ********************* // **** check all elements of the form ***/function checkform (formname) {var oform = document. all (formname); var eles = oform. elements; // var els // traverse all table elements for (VAR I = 0; I <eles. length; I ++) {// whether to verify var stype = eles [I]. valuetype; If (stype) {If (eles [I]. mustinput! = NULL & eles [I]. mustinput = "true") {// els = eles [I]. value; // els = trim (ELS); If (TRIM (eles [I]. value) = "") {If (eles [I]. objname! = NULL) {alert (eles [I]. objname + "cannot be blank");} else {alert ("this text box is a mandatory field");} eles [I]. focus (); event. returnvalue = false; return false ;}} switch (stype) {// integer case "int": If (! Validint (eles [I]) {event. returnvalue = false; return false;} break; // fractional case "float": If (! Validfloat (eles [I]) {event. returnvalue = false; return false;} break; // string case "string": If (! Validstring (eles [I]) {event. returnvalue = false; return false;} break; // Date case "date": If (! Validdate (eles [I]) {event. returnvalue = false; return false;} break; // Mail case "email": If (! Validemail (eles [I]) {event. returnvalue = false; return false;} break; // single choice button/* case "radio": If (! Validradio (eles [I]) {event. returnvalue = false; return false;} break; // check button case "checkbox": If (! Validbox (eles [I]) {event. returnvalue = false; return false;} break; * // drop-down list box case "select": If (! Validselect (eles [I]) {event. returnvalue = false; return false;} break; // list box case "list": If (! Validlist (eles [I]) {event. returnvalue = false; return false;} break ;}} event. returnvalue = true; return true;}/*** verify whether it is an integer ***/function validint (Ele) {If (! Isint (Ele. Value) {alert ("enter a valid integer"); ELE. Focus (); Return false;} else {If (Ele. maxinput! = NULL &&! Isnan (Ele. maxinput) if (parseint (Ele. maxinput) <parseint (Ele. value) {alert ("your input" + convertnulltospace (Ele. objname) + "value should be less than" + ELE. maxinput); ELE. focus (); Return false;} If (Ele. mininput! = NULL &&! Isnan (Ele. mininput) if (parseint (Ele. mininput)> parseint (Ele. value) {alert ("your input" + convertnulltospace (Ele. objname) + "value should be greater than" + ELE. mininput); ELE. focus (); Return false ;}} return true;}/*** determines whether it is an integer ***/function isint (s) {var patrn =/^ 0 | ^ [1-9] D */; If (! Patrn.exe C (s) {return false;} else {return true;}/*** verify whether it is a decimal ***/function validfloat (Ele) {If (isnan (Ele. value) {alert ("enter a valid number"); ELE. focus (); Return false;} else {If (Ele. decimallen! = NULL &&! Checkdecimal (Ele. value, ELE. decimallen) {alert ("your input" + convertnulltospace (Ele. objname) + "the maximum number of decimal places is" + ELE. decimallen + "decimal places"); ELE. focus (); Return false;} If (Ele. maxinput! = NULL &&! Isnan (Ele. maxinput) if (parseint (Ele. maxinput) <parseint (Ele. value) {alert ("your input" + convertnulltospace (Ele. objname) + "value should be less than" + ELE. maxinput); ELE. focus (); Return false;} If (Ele. mininput! = NULL &&! Isnan (Ele. mininput) if (parseint (Ele. mininput)> parseint (Ele. value) {alert ("your input" + convertnulltospace (Ele. objname) + "value should be greater than" + ELE. mininput); ELE. focus (); Return false ;}} return true;}/*** verify whether it is a string ***/function validstring (Ele) {If (Ele. stringlen! = NULL &&! Isnan (Ele. stringlen) {VaR value = new string (Ele. value); If (value. length> parseint (Ele. stringlen) {alert ("your input" + convertnulltospace (Ele. objname) + "the maximum word length is" + ELE. stringlen); ELE. focus (); Return false ;}} return true;}/*** verify whether the date format is ***/function validdate (Ele) {If (! Isdate (Ele. Value) & ELE. value! = "") {Alert ("Enter the valid date (yyyy-mm-dd)"); ELE. focus (); Return false;} return true;}/*** determines whether the date is ***/function isdate (STR) {var r = Str. match (/^ (d {1, 4}) (-|/) (d {1, 2}) 2 (d {1, 2})/); If (r = NULL) {return false;} var d = new date (R [1], R [3]-1, R [4]); If (! (D. getfullyear () = R [1] & (D. getmonth () + 1) = R [3] & D. getdate () = R [4]) {return false;} return true;}/*** verify whether the email address is ***/function validemail (Ele) {If (! Isemail (Ele. value) {alert ("enter a valid Email Address"); ELE. focus (); Return false;} return true;}/*** determines whether the email address is ***/function isemail (STR) {If (Str. match (/[w-.] + @ {1} [w-] +. {1} W {2, 4 }(. {0, 1} W {2}) {0, 1}/ig )! = Str) return false; else return true;}/*** whether to select the ***/function validradio (Ele) {// var rads = document. getelementsbyname (Ele. name); EVAL ("Var rads =" + name + ". "+ ELE. name); var selectcount = 0; For (VAR I = 0; I <rads. length; I ++) {If (rads [I]. checked) {selectcount ++;} If (Ele. mustselect! = NULL & ELE. mustselect) {If (selectcount = 0) {alert ("select" + convertnulltospace (Ele. objname); ELE. focus (); Return false ;}} return true;}/*** verify whether the check button needs to select ***/function validbox (Ele) {// var rads = document. getelementsbyname (Ele. name); EVAL ("Var chks =" + name + ". "+ ELE. name); var selectcount = 0; For (VAR I = 0; I <chks. length; I ++) {If (chks [I]. checked) {selectcount ++;} If (Ele. minselect! = NULL &&! Isnan (Ele. minselect) {If (selectcount <parseint (Ele. minselect) {alert (convertnulltospace (Ele. objname) + "select at least" + ELE. minselect + "item"); ELE. focus (); Return false ;}}if (Ele. maxselect! = NULL &&! Isnan (Ele. maxselect) {If (selectcount> parseint (Ele. maxselect) {alert (convertnulltospace (Ele. objname) + "select at most" + ELE. maxselect + "item"); ELE. focus (); Return false ;}} return true;}/*** verify whether the drop-down list box needs to select ***/function validselect (Ele) {// var rads = document. getelementsbyname (Ele. name); If (Ele. mustselect! = NULL & ELE. mustselect) {If (Ele. selectedindex = 0) {alert ("select" + convertnulltospace (Ele. objname); ELE. focus (); Return false ;}} return true;}/*** verify the number of items selected in the list box ***/function validlist (Ele) {// var rads = document. getelementsbyname (Ele. name); var selectcount = 0; For (VAR I = 0; I <ELE. options. length; I ++) {If (Ele. options [I]. selected) {selectcount ++;} If (Ele. minselect! = NULL &&! Isnan (Ele. minselect) {If (selectcount <parseint (Ele. minselect) {alert (convertnulltospace (Ele. objname) + "select at least" + ELE. minselect + "item"); ELE. focus (); Return false ;}}if (Ele. maxselect! = NULL &&! Isnan (Ele. maxselect) {If (selectcount> parseint (Ele. maxselect) {alert (convertnulltospace (Ele. objname) + "select at most" + ELE. maxselect + "item"); ELE. focus (); Return false ;}} return true;}/*** converts null to a space to display the object name ***/function convertnulltospace (paramvalue) {If (paramvalue = NULL) Return ""; else return paramvalue;}/*** check decimal places ***/function checkdecimal (Num, decimallen) {var Len = decimallen * 1 + 1; if (Num. indexof ('. ')> 0) {num = num. substr (Num. indexof ('. ') + 1, num. length-1); If (Num. length) <Len) {return true;} else {return false;} return true;}/*** remove space ***/function trim (STR) {If (Str. length> 0) {While (Str. substring (0, 1) = "") & (Str. length> 0) {STR = Str. substring (1, str. length);} while (Str. substring (Str. length-1, str. length) = "") {STR = Str. substring (0, str. length-1) ;}} return STR ;} |