jquery Form Verification plug-in usage and instance

Source: Internet
Author: User
Tags extend php tutorial

<!doctype HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/ Xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title></title>

&lt;script&gt;


Jquery.extend ({


Options: {


Ctrls: [],//controls to check


Success:function () {return;},//when check success,you can do Something,such as submit a AJAX request


Failed:function (msg, id) {jquery.clewmsg (msg, id);}//when Check Faild


},





Clewmsg:function (msg, id) {


Alert (msg);


},





Checkform:function (o) {


o = Jquery.extend ({},jquery.options,o);





var isok = http://www.111cn.net/wy/jquery/true;








var flashprompt = function (CTR) {


var i = 0;


var intervalid = setinterval (function () {


jquery ("#" +ctr.id). Toggleclass (' warning ');


if (i++ &gt; 2) {


Clearinterval (Intervalid);


jquery ("#" +ctr.id). addclass (' warning ');


}


}, 100);


};





Check failed, we alert a message, and change the control ' s style


var fail = function (CTR) {


isOK = false;


O.failed (ctr.msg, ctr.id);


Flashprompt (CTR);


jquery ("#" +ctr.id). focus ();


return false;


}





Check success, we change the control to its original style


var succ = function (CTR) {


jquery ("#" +ctr.id). Removeclass (' warning ');


return http://www.111cn.net/wy/jquery/true;


}





Regular Express Check


var checkregularexpression = function (val, expression) {


if (Val!= "")


{


var Matcharray = val.match (expression);


if (Matcharray = null) return false;else return http://www.111cn.net/wy/jquery/true;


}


else return http://www.111cn.net/wy/jquery/true;


}





Jquery.each (O.ctrls, function (i, CTR) {


Switch (ctr.type)


{


Case "Mustinput": if (jquery ("#" +ctr.id). val () = "") Return fail (CTR); else return succ (CTR);


Case "Mustmorethan": if (jquery ("#" +ctr.id). Val (). length &lt; Ctr.par) return fail (CTR); else return succ (CTR);


Case "Mustlessthan": if (jquery ("#" +ctr.id). Val (). length &gt; Ctr.par) return Fail (CTR); else return succ (CTR);


Case "Mustequalto": if (jquery ("#" +ctr.id). Val ()!= jquery ("#" +ctr.par). Val ()) return fail (CTR);


Case "Mustemail": if (!checkregularexpression) (jquery ("#" +ctr.id). Val (),/w+ ([-+.] w+) *@w+ ([-.] w+) *.w+ ([-.] w+)) return Fail (CTR), Else return succ (CTR);


Case "Mustint": if (!checkregularexpression (jquery ("#" +ctr.id). Val (),/^[0-9]*jquery/)] return fail (CTR); SUCC (CTR);


Case "Mustfloat": if (!checkregularexpression) (jquery ("#" +ctr.id). Val (),/^[0-9]+.{ 0,1}[0-9]{0,2}jquery/)) return Fail (CTR), Else return succ (CTR);


Case "Mustselect": if (jquery ("#" +ctr.id). val () = = Ctr.par) return fail (CTR); else return succ (CTR);


Case "MustCheck": if (!jquery ("#" +ctr.id), attr ("checked")) return fail (CTR); else return succ (CTR);


Case "Mustradio": if (jquery ("Input[type= ' Radio '][name= '" +ctr.id+ "']:checked '), length&lt;1) return fail (CTR); else return succ (CTR);


Case "Mustregular": if (!checkregularexpression (jquery ("#" +ctr.id). Val (), Ctr.par)) return fail (CTR); else return succ ( CTR);


}


});








if (isOK) o.success ();


}


});

</script>

<body>

&lt;form id= "Form1" runat= "Server" &gt;


&lt;div&gt;


&lt;div id= "divmsg" style= border-right:deeps course kyblue thin; Display:none; margin:0; padding:0; Border-top:deepskyblue thin solid;


Border-left:deepskyblue thin solid; width:452px; Color: #3300ff; Border-bottom:deepskyblue thin solid;


Height:27px "&gt;


&lt;/div&gt;


&lt;table style= "width:455px" &gt;


&lt;tr&gt;


&LT;TD colspan= "2" &gt;


Jion to Us&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&lt;td&gt;


Username:&amp;nbsp;&lt;/td&gt;


&lt;td&gt;


&lt;input id= "txtUserName" type= "text"/&gt;&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&lt;td&gt;


Password:&lt;/td&gt;


&lt;td&gt;


&lt;input id= "txtpassword" type= "password"/&gt;&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&lt;td&gt;


And once again?&amp;nbsp;


&lt;br/&gt;


&lt;/td&gt;


&lt;td&gt;


&lt;input id= "Txtpasswordagain" type= "text"/&gt;&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&lt;td&gt;


And your sex?&lt;/td&gt;


&lt;td&gt;


&lt;input id= "Radio1" type= "Radio" name= "Rdosex"/&gt;


Man


&lt;input id= "Radio2" type= "Radio" name= "Rdosex"/&gt;


Female &lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&lt;td&gt;


Your telephone:&lt;/td&gt;


&lt;td&gt;


&lt;input id= "Txttelephone" type= "text"/&gt;&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&lt;td&gt;


Your email:&lt;/td&gt;


&lt;td&gt;


&lt;input id= "Txtemail" type= "text"/&gt;&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&lt;td&gt;


Your age:&lt;/td&gt;


&lt;td&gt;


&lt;input id= "Txtage" type= "text"/&gt;&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&lt;td&gt;


Your weight:&lt;/td&gt;


&lt;td&gt;


&lt;input id= "Txtweight" type= "text"/&gt;&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&lt;td&gt;


Country:&lt;/td&gt;


&lt;td&gt;


&lt;select id= "Selcountry" &gt;


&lt;option value= "0" &gt;select&lt;/option&gt;


&lt;option value= "1" &gt; China &lt;/option&gt;


&lt;option value= "2" &gt; USA &lt;/option&gt;


&lt;option value= "3" &gt; UK &lt;/option&gt;


&lt;/select&gt;


&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&lt;td&gt;


Remark:&lt;/td&gt;


&lt;td&gt;


&lt;textarea id= "Txtremark" cols= "rows=" 2 "&gt;&lt;/textarea&gt;&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&lt;td&gt;


&lt;/td&gt;


&lt;td&gt;


&lt;input id= "Chkagree" type= "checkbox"/&gt;i agree all the terms.&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&lt;td&gt;


&lt;/td&gt;


&lt;td&gt;


&lt;input id= "button1" type= "button" value= "Check form" onclick= "Checkform ()"/&gt;&lt;/td&gt;


&lt;/tr&gt;


&lt;/table&gt;


&lt;/div&gt;

</form>




Rule description: Required:http://www.111cn.net/wy/jquery/true must fill in


MinLength: Minimum Length


MaxLength: Maximum length


Rangelength: [3,10] string between 3 and {1}


RANGE:[100,1000] can only be a value between 100 and 1000.


Min: Minimum value


Max: Max value


Email:http://www.111cn.net/wy/jquery/true Verify Mailbox


Url:http://www.111cn.net/wy/jquery/true Verify that the URL is valid


Date:http://www.111cn.net/wy/jquery/true Verify is valid date new date () type format


Dateiso:http://www.111cn.net/wy/jquery/true Verify is valid date year/month/day or year-month-day format


Number:http://www.111cn.net/wy/jquery/true Verify that the number is valid


Digits:http://www.111cn.net/wy/jquery/true validation is an integer


CreditCard: Verify the legal credit card number


Equalto: "To match the elements" such as: ' #cnfpass ', verify that two input values are the same


Accept: "Gif|png|jpg" verifies if the string is a legal suffix name


Remote: ' URL ' remotely verify that the value entered is valid URL is returned http://www.111cn.net/wy/jquery/true validation succeeded, false validation failed


These are the validation rules provided in the plug-in, and the Plug-ins provide a way to add rules to add some of our own rules, such as:


Jquery.validator.addmethod ("rule name", function (value, element, params) {return this.optional (Element) | | Verify Value}


b) Using the Jquery.validate Validate method to validate





&lt;script&gt; $ (function () {&nbsp; $ (' #frm '). Validate ({&nbsp; rules:{&nbsp; username:{&nbsp; required:http:// www.111cn.net/wy/jquery/true,&nbsp; minlength:4,&nbsp; Remote: ' check_username.php tutorial ' &nbsp;},&nbsp; password:{ &nbsp; required:http://www.111cn.net/wy/jquery/true,&nbsp; minlength:6,&nbsp; equalto: ' #cnfpass ' &nbsp;},&nbsp; cnfpass:{&nbsp; required:http://www.111cn.net/wy/jquery/true&nbsp;}&nbsp;},&nbsp; messages:{&nbsp; username:{ &nbsp; Required: ' Required ',&nbsp; minlength: ' Minimum length 3 ',&nbsp; remote: ' already exists ' &nbsp;},&nbsp; password:{&nbsp; required: ' Required ', &nbsp; minlength: ' Minimum length 6 ',&nbsp; equalto: ' Two input passwords inconsistent ' &nbsp;},&nbsp; cnfpass:{&nbsp; required: ' Required ' &nbsp;}&nbsp;}, &nbsp; Submithandler:function (form) {&nbsp;&nbsp; form.submit ()/Note this place cannot use $ (from) .submit&nbsp;}&nbsp;}) &nbsp;}) &nbsp; &lt;/script&gt; &lt;form name= "frm" id= "frm" method= "post" action= "" &gt; &lt;input type= "text" name= "username" &gt; &lt;input type= "text" name= "password" &gt; &lt;input type= "text" name= "Cnfpass" &gt; &lt;input tyPe= "Sumbit" value= "Submit" &gt; &lt;/form&gt;


Let's take a look at the jquery Checkform plug-in simple usage

$ (function () {

$ ("form"). Valid ([

Validation selection

{Name: ' checkbox ', simple: ' Multiple selections '},

Verify required, as long as it is not empty

{Name: ' username ', simple: ' username '},

Non-required, mailbox type

{Name: "email", type: "Mail", Simple: "Mailbox", Require:false}

]);

})

</body>

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.