Universal form verification function

xiaoxiao2021-03-06  29

Check.js JS function file / * * --------------- Client table single pass authentication Checkform (OFORM) ----------------- * Function: General Verification All Form Elements. * Use: *

* * * author: wanghr100 (gray baby .NET) * Email: Wanghr100@126.com * Update: 19: 28 2004-8-23 * Note: Be careful when writing regular expressions. Don't let "people" have an empty diamond. * Has implemented: * For Text, Password, Hidden, File, Textarea, SELECT, Radio, Checkbox for legality verification * Waiting for the implementation: write the regular expression into a library. * --------------- Client table single pass Verification Checkform (OFORM) ----- ------------ * /

// 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 ();}} DEMO.HTM demo file