A very popular form verification code

xiaoxiao2021-03-06  15

/ * * --------------- Client Table Single CONQERY TECHNICAL CHECKFORM (Ofee) ----------------- * Function: General Verification All Form element. * Use: *

* * * * * * * has implemented: * For Text, Password, Hidden, File, Textarea, Select, Radio, Checkbox Perform legitimacy verification * * --------------- Client table single pass verification Checkform (OFORM) ----------------- * / / ** * Regular expression use case * 1, ^ / s [az az] $ can't have spaces can only be English letters * 2, / s {6,} can't be empty six or more * 3, ^ / d $ Can't have spaces that cannot be not numbers * 4, (. *) (/. Jpg | /.bmp) can only be JPG and BMP format * 5, ^ / d {4} / - / d {1, 2} - / D {1,2} $ can only be 2004-10-22 format * 6, ^ 0 $ at least one item * 7, ^ 0 {2,} $ at least two items * 8, ^ [/ s | / S] {20,} $ 不 不 二 二 二 字 字 * 9, ^ / ? [A-Z0-9] (([- .] | [_] )? [A-Z0-9] ) * @ ([A-Z0-9] (/. | / -)) [AZ] {2,6} $ Email * 10, / W ([- .] / w ) * @ / w ([[[[ -.] / w ) * /. / w ([-.] / w ) * ([,;] / s * / w ([- .] / w ) * @ / w ([-.] / w ) * /. / w ([-.] / w ) *) * Enter multiple addresses to separate emails with commas or spaces * 11, ^ (/ ([0-9] /))? [0-9] { 7, 8} $ 25 or 8 or 8 orchestral code, for example (022) 87341628 * 12, ^ [AZ AZ 0-9 _] @ [AZ AZ 0-9 _ ] (/. [AZ AZ 0-9 _] (/, [AZ AZ 0-9 _] @ [AZ AZ 0-9 _] (/. [AZ AZ 0-9 _] ) ) * $ * Can only be letters, numbers, underscores; must have @ and. Simultaneous format to specify email * 13 ^ / w @ / w (/. / W ) (/, / w @ / w (/ ./w ) ) * The above expressions can also be written, more refined. 14 ^ / W ((- / W ) | (/. W )) * / @ / w ((/. | -) / w ) * /. / W $

* * * * * * * * * * /

// Main function function checkform ({var els = = = lyments; // Traverse all table elements for (var i = 0; i Regular expression, not case sensitive varreg = new regexp (SREG, "I"); if (! Reg.test (sval)) {// Verification does not pass, pop-up prompt warning alert (ELS [i] .warning ); // The form element acquires the focus, uses a universal return function GOBACK (ELS [I]) Return False;}}}}}

/ / Universal Value Function is divided into three categories // text input box, directly values ​​EL.Value // Single multi-selection, traverse all options acquire the number of selected numbers "00" indicates that two /// Single plummete menu, traversing all options acquired the number of selected numbers Returns "0" indicates the type of Function getValue (EL) {// Var style = el.Type; switch (stype) {casse "text ": Case" hidden ": case" password ": case" file ": case" textarea ": return el.value; Case" Checkbox ": Case" Radio ": Return GetValuechoose (EL); Case" Select-One ": Case "select-multiple": return getValueSel (EL);} // acquire radio, checkbox's selection, use "0" to indicate the number of selected numbers, we can pass 0 {1,} when we write Select a number of function getValuechoose (el) {var svalue = ""; // get the NAME of the first element, search this element group VAR TMPELS = Document.GtelementsByname (El.Name); for (var i = 0; i < TMPELS.LENGTH; I ) {IF (TmPels [i] .checked) {sValue = "0";}} Return Svalue;} // Get selected in SELECT, use "0" to represent the number of selected numbers, we When writing is correct, you can use 0 {1,} to indicate a number of function getValueSel (el) {var svalue = ""; for (var i = 0; i

// General return function, verify that the effect is not returned. Take three types of value // text input box, the cursor is positioned in the end of the text input box // Single multi-selection, the first option acquisition focus // Single drop menu , Acquire FUNCTION GOBACK (EL) {// Type VAR Stype = El.Type; Switch (STYPE): Case "Hidden": Case "Password": Case "File": case " TEXTAREA ": El.focus (); var rng = el.createtextRange (); rng.collapse (false); rng.select (); Case" Checkbox ": Case" Radio ": var els = document.getElementsByName (el.) Name); ELS [0] .focus (); case "select-one": case "select-multiple": el.focus ();}} // check FieldFunction checkfield (file) {// Do you need to verify IF ( Field.check) {// Validated regular string var sag = field.check; // Get the value of the form, use universal values ​​function var sval = getValue (field); // String -> Regular expression, Not case sensitive varreg = new regexp (SREG); if (! Reg.test (sval)) {// Verification is not passed, pop-up prompt warning alert (field.warning); // This form element achieves focus, with universal Return Function GOBACK (Field) Return false;}} return true;} attached: regular expression list

转载请注明原文地址:https://www.9cbs.com/read-46107.html

New Post(0)