Jquery plugin Verification Form

Source: Internet
Author: User

Form Verification is completed based on jquery. You can define verification rules or custom verification methods.

 

The source code is as follows;

(Function ($) {<br/> $. FN. extend ({<br/> fame: function () {<br/> var r_value = true; <br/> var r_hanle = true; <br/> If (this. find (": Input "). size () = 0) {<br/> return true; <br/>}< br/> This. find (": Input "). each (function () {<br/> if ($ (this ). ATTR ("filter") = "true") {<br/> var kinds = $ (this ). ATTR ("kinds"); <br/> var kindsarray = kinds. split (","); <br/> var tips = $ (this ). ATTR ("Tips"); <br/> var tipsarray = tips. split (","); <br/> for (var c in kindsarray) {<br/> If (kindsarray [c] = "null ") {<br/> $ ("# tip", $ (this ). parent ()). remove (); <br/> if ($. del_space ($ (this ). val () = "") {<br/> $. process_tip ($ (this), tipsarray [c]); <br/> r_value = false; <br/> return false; <br/>}< br/> If (kindsarray [c] = "integer") {<br/> $ ("# tip ", $ (this ). parent ()). remove (); <br/> If (isnan ($ (this ). val () {<br/> $. process_tip ($ (this), tipsarray [c]); <br/> r_value = false; <br/> return false; <br/>}</P> <p >}); <br/> This. find ("*"). each (function () {<br/> if ($ (this ). ATTR ("handle") = "true") {<br/> If (r_value) {<br/> handle_fn = eval ("(" + $ (this ). ATTR ("FN") + "); <br/> r_hanle = handle_fn (); <br/> $ (" # tip ", $ (this ). parent ()). remove (); <br/> $. process_tip ($ (this), ""); <br/>}< br/>}); <br/> If (this. find ("# tip "). size () = 0) {<br/> r_value = true; <br/> r_hanle = true; <br/>}< br/> return r_value & r_hanle; <br/>}</P> <p >}); <br/> $. extend ({<br/> process_tip: function (_ this, tip) {<br/> _ this. parent (). append ("<span id = 'tip '> <font color = 'red'>" + tip + "</font> </span> "); <br/>}, <br/> del_space: function (SRC) {<br/> var Reg = // s/g; <br/> return SRC. replace (Reg, ""); <br/>}< br/>}); <br/>}< br/>) (jquery); <br/>

 

The test code is as follows:

 

<HTML> <br/> <MCE: script src = "jquery-1.4.2.js" mce_src = "jquery-1.4.2.js"> </MCE: SCRIPT> <br/> <MCE: script src = "form_pluan.js" mce_src = "form_pluan.js"> </MCE: SCRIPT> <br/> <MCE: Script Type = "text/JavaScript"> <! -- <Br/> $ (document ). ready (function () {<br/> $ ("# "). submit (function () {<br/> return $ ("# ")). fame (); </P> <p >}); <br/> function sel () {<br/> if ($ ("select: eq (0) option: Selected "). val () = "-1") {<br/> alert ("Select! "); <Br/> return false; <br/>}< br/> // --> </MCE: SCRIPT> <br/> <body> <br/> <Form ID = "A" Action = ""> <br/> <Table> <tr> <TD> <br /> <input type = "text" filter = "true" kinds = "null, integer "Tips =" can not be null, must be integer "/> </TD> <br/> </tr> <br/> <tr> <TD> <br/> <input type =" text "Filter = "true" kinds = "integer, null "Tips =" must be integer, can not be empty "/> <br/> </TD> </tr> <br/> <tr> <TD> <br/> <select handle =" true" fn = "Sel"> <br/> <option value = "-1"> select </option> <br/> <option value = "1"> fame </ option> <br/> <option value = "2"> fame </option> <br/> </SELECT> <br/> </TD> </tr> <br/> <input type = "Submit" id = "St"/> <br/> </form> <br/> </body> <br/> </ptml> <br/>

 

Note:

 

<Form> </form> label <input/> label defined inFilter = "true"
Indicates that you need to use a plug-in for verification,Kinds = "null"
Indicates that non-empty verification is used,Kinds = "integer"
Verification is a number. You can also perform multiple verification. For example:Kinds = "null, integer"
, Separated.Tips = "MSG"
Indicates the prompt information to be displayed when the verification fails. It is used when multiple verifications exist.Tips = "msg1, msg2"
, Separated by commas. Tips corresponds to kinds. For example, <input type = "text" filter = "true" kinds = "null, integer" Tips = "can not be null, must be integer"/>, null corresponds to can not be null, and integer corresponds to must be integer.


Label DefinitionHandle = "true"
Indicates using a custom method for verification,Fn = "Sel"
SEL is the method name you defined. In your custom method, false must be returned | true.

 

 

Page call;

 

Write a form. After loading the page, register an event for the form you want to verify. The Code is as follows:

$ ("# "). Submit (function () {<br/> return $ ("# ")). fame (); <br/> });

 

Shown as follows:

1. Kind = "null, integer"

2. Kind = "null, integer"

 

3. Handle = "true" fn = "Sel" Custom verification method. Used in the drop-down box.

Many functions are being improved...

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.