We recommend a self-encapsulated javascript plug-in that encapsulates the javascript plug-in.
For details, please refer to the notes. There will be no more BB here,
Code:
/// <Reference path = "vendor/jquery-1.4.1-vsdoc.js"/> // The detection form cannot be empty (. notnull) validation/* Time: 2012-6-6 function: when multiple (including one) forms under a pair of form tags need to be submitted, use js to accurately determine the elements using the current button: Find the container of the current form under the form tag and give class = "form ", the submit button of the current form gives the class = "check" element that needs to be verified as null to the class = "notnull" nullmsg = "xx cannot be blank! "Prompt: the form for logical judgment should be given to class =" need ", and the type of judgment should be given to class =" num "(only numbers are allowed) verification prompt: logicmsg = "XX can only be a number" Give class = "errorMessage" Show ERROR information block give class = "warn" Show ERROR information do not use js object-oriented programming logic judgment, without passing in the need identifier, you can directly give the regular expression attribute (custom) regex = "/^ \ d $/" to determine whether the Global is implemented externally. the submitCallback button callback function is Global. confirmCallback confirm callback function; to be improved: No Update Time: December 3, 2014 16:23:22 Author: Amber. xu * // $ (document ). ready (// function () {// $ ("form "). find (". notnull "). bind ({// focu S: function () {// if ($ (this ). attr ("value") = this. defaultValue) {// $ (this ). attr ("value", ""); //}, // blur: function () {// if ($ (this ). attr ("value") = "") {// $ (this ). attr ("value", this. defaultValue); //} //); // * encapsulate the 10 thousand method for detecting the form * // event. srcElement: the target object that triggers the event. It is often used for onclick events. /// Event. fromElement: the object source that triggers the event. It is often used for onmouseout and onmouseover events. /// Event. toElement: The target source to which the mouse moves after an event is triggered. It is often used for onmouseout and onmouseover events. Function Global () {var _ self = this;} Global. submitCallback = null; Global. confirmCallback = null; $ (document ). ready (function () {// form body $ ("body "). find (". form "). each (function () {this. onclick = function (e) {var button = null; try {button = e. srcElement = null? Document. activeElement: e. srcElement;} catch (e) {console. log (e. message) button = document. activeElement;} if ($ (button ). is (". check ") {// alert (" Submit ") var sub = (checkform (this) & CheckInputRex (this) & checkselect (this) & checkChecked (this); if (sub) {// Call our callback, but using our own instance as the context Global. submitCallback. call (this, [e]);} return sub;} else if ($ (button ). I S (". confirm ") {// alert (" delete ") var sub = confirm ($ (button ). attr ("title"); if (sub) {Global. confirmCallback. call (this, [e]);} return sub;} else {// alert ("other") return true ;}}}); /* elements in the detection form that cannot be empty */function checkform (form) {var B = true; $ (form ). find (". notnull "). each (function () {if ($. trim ($ (this ). val ()). length <= 0) {// | $ (this ). val () = this. defaultValue // if (this. value! = Null) {// $ (this ). attr ("value", ""); // alert ($ (this ). attr ("msg") $ (this ). parents (". form "). find (". warn "). text ($ (this ). attr ("nullmsg"); $ (this ). parents (". form "). find (". errorMessage "). show (); $ (this ). select (); $ (this ). focus (); return B = false ;}}); if (B = true) {$ (form ). find (". warn "). text (""); $ (form ). find (". errorMessage "). hide ();} return B;}/* required drop-down list in the detection form */function checkselect (form) {Var B = true; $ (form ). find (". select "). each (function (I) {var ck = $ (this ). find ('option: selected '). text (); if (ck. indexOf ("select")>-1) {$ (this ). parents (". form "). find (". warn "). text ($ (this ). attr ("nullmsg"); $ (this ). parents (". form "). find (". errorMessage "). show (); $ (this ). select (); $ (this ). focus (); return B = false ;}}); return B ;}/ * check box */function checkChecked (form) {var B = true; $ (fo Rm). find (". checkbox"). each (function (I) {var ck = $ (this) [0]. checked; if (! Ck) {$ (this ). parents (". form "). find (". warn "). text ($ (this ). attr ("nullmsg"); $ (this ). parents (". form "). find (". errorMessage "). show (); $ (this ). select (); $ (this ). focus (); return B = false ;}}); return B ;}// check whether the regular expression function GetFlase (value, reg, ele) {if (reg. test (value) {return true;} $ (ele ). parents (". form "). find (". warn "). text ($ (ele ). attr ("logicmsg"); $ (ele ). parents (". form "). find (". errorMess Age "). show (); $ (ele ). focus (); $ (ele ). select (); return false; // cannot submit} function CheckInputRex (form) {var B = true; $ (form ). find ("input [type = 'text']"). each (function () {if (typeof ($ (this ). attr ("regex") = 'string') {if ($. trim ($ (this ). val ()). length> 0 & $ (this ). val ()! = This. defaultValue) {// var value of the current form = $ (this ). attr ("value") | $ (this ). val (); var regx = eval ($ (this ). attr ("regex"); return B = GetFlase (value, regx, this) ;}}); return B ;} /// check whether the corresponding characters entered by the user are valid. // function CheckInput (form) {var B = true has been discarded in this method. $ (form ). find (". need "). each (function () {if ($. trim ($ (this ). val ()). length> 0 & $ (this ). val ()! = This. defaultValue) {// var value of the current form = $ (this ). attr ("value"); // id value or name attribute value, for example, [name = "contact"] var name = $ (this ). attr ("class"); // check whether the content to be entered is legal, for example, contact information var len = name. split (""); for (var I = 0; I <len. length; I ++) {switch ($. trim (len [I]) {// Contact Information case "mobile": var reg =/^ 1 \ d {10} $/; return B = GetFlase (value, reg, this); break; // email case "email": var reg =/^ [\ w-] + (\. [\ w-] +) * @ [\ w-] + (\. [\ W-] +) + $/; return B = GetFlase (value, reg, this); break; // whether the two passwords are consistent case "password": break; case "password2": if ($ ("# password "). attr ("value ")! = $ ("# Password2 "). attr ("value") {$ (this ). select (); // obtain the focus $ (this ). parents (". form "). find (". warn "). text ($ (this ). attr ("logicmsg"); $ (this ). parents (". form "). find (". errorMessage "). show (); return B = false; // cannot submit} break; case "worktel": case "hometel ": // home phone var reg =/^ \ d {8} $/; return B = GetFlase (value, reg, this); break; case "post ": // zip code var reg =/^ \ d {6} $/; return B = GetFlase (value, reg, this ); Break; case "bonus": case "allowance": case "FixedSalary": var reg =/^ -? ([1-9] \ d * \. \ d * | 0 \. \ d * [1-9] \ d * | 0? \. 0 + | 0 | [1-9] \ d) $/; return B = GetFlase (value, reg, this); break; case "identity ": var reg =/(^ \ d {15} $) | (^ \ d {18} $) | (^ \ d {17} (\ d | X | x) $)/; return B = GetFlase (value, reg, this); break; case "height ": var reg =/^ [1-2] [0-9] [0-9] $/; return B = GetFlase (value, reg, this); break; case "qq": var reg =/^ [1-9] [0-9] {4,} $/; return B = GetFlase (value, reg, this ); break; case "begintime": case "endtime": var reg =/^ \ d {4 }$/; if (reg. test (value) & (parseInt ($ (". endtime "). val ()> parseInt ($ (". begintime "). val () {return B;} $. ligerDialog. alert ($ (this ). attr ("msg") $ (this ). select (); // get focus return B = false; // cannot submit break; case "num": var reg =/^ \ d + $ /; return B = GetFlase (value, reg, this); break; // you need to apply for a Hong Kong and Macao pass and a signature for Hong Kong to go to Hong Kong. the format of the private general Passport number is: // 14/15 + 7 digits, G + 8 digits; // for public general: P. + 7 digits; // The official business is: S. + 7-digit or // S + 8-digit diplomatic passport case "postport" starting with "D ": // passport number var reg =/^ (P \ d {7} | G \ d {8} | S \ d {7, 8} | D \ d + | 1 [4, 5] \ d {7 }) $/; return B = GetFlase (value, reg, this); break; case "bankaccount": var reg =/^ [0-9] {19} $ /; return B = GetFlase (value, reg, this); break;} // switch} // for}); return B ;}/// this method has been discarded }); /// click to change the background color $ (document ). ready (function () {var inputs = $ ("# top>. c> input "); $ (inputs ). each (function () {this. onclick = function () {document. getElementById ("main "). style. backgroundColor = this. name; // $ ("# main "). backgroundColor = this. name ;}});});
Basically, common functions are encapsulated. I hope my friends will like them.