JavaScript Form Check

xiaoxiao2021-03-06  15

/ *

* --------------- Client Table Single Condition Validation Checkform (OFORM) -----------------

* Function: GM verify all form elements.

* Use:

*

*

*

*

*

*

*

* Functions have been implemented:

* Legal feature verification for Text, Password, Hidden, File, Textarea, SELECT, RADIO, CHECKBOX

*

* --------------- Client Table Single Condition Validation Checkform (OFORM) -----------------

* /

/ **

* Regular expression use case

* 1, ^ / s [a-z a-z] $ can't have space, can only be English letters

* 2, / s {6,} can't be empty than six or more

* 3, ^ / d $ can't have spaces No 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} $ message

* 10, / w ([- .] / W ) * @ / w ([-.] / W ) * /. / W ([-.] / W ) * ([,;] / s * / w ([- .] / w ) * @ / w ([-.] / w ) * /. / w ([-.] / w ) *) * Enter multiple addresses with commas or space separated mail

* 11, ^ (/ ([0-9] /))? [0-9] {7,8} $ Phone Number 7 or 8 orchestral Area 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 emails

* 13 ^ / w @ / w (/. / w ) (/, / w @ / w (/.) * $ Above the expression 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;

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

New Post(0)