在開發(fā)中用到了jQuery的validate控件,有時(shí)需要自定義驗(yàn)證方法。我們可以通過jQuery.validator.addMethod()來實(shí)現(xiàn),下面是例子:
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <link href="../css/main.css" rel="stylesheet" type="text/css" />
- <link href="../js/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
- <!-- 引入JS庫(kù)文件 -->
- <script src="../js/jquery.min.js" type="text/javascript"></script>
- <script src="../js/ligerUI/js/ligerui.min.js" type="text/javascript"></script>
- <script src="../js/tablecolor.js" type="text/javascript"></script>
- <script src="../js/jquery-form/jquery.form.js" type="text/javascript"></script>
- <script src="../js/jquery-validation/jquery.validate.min.js" type="text/javascript"></script>
- <script src="../js/jquery-validation/messages_cn.js" type="text/javascript"></script>
-
- <script type="text/javascript">
- <!--
- $(function() {
-
- //保存
- $("#appEdit_btok").click(function () {
- $("#appEdit_Form").submit();
- });
-
- //自定義評(píng)語(yǔ)內(nèi)容驗(yàn)證方法
- jQuery.validator.addMethod("levelLimit",function(value, element){
- var returnVal = false;
- var level = $("#code").attr("level");
- if(level=="1" || level=="0"){
- returnVal = true;
- }
- return returnVal;
- },"level不能為2!");
- $("#appEdit_Form").validate({
- rules : {
- "code" : {
- required : true,
- number : true,//期望的是true,如果為false則展示提示信息
- levelLimit : true//期望的是true,如果為false則展示提示信息
- }
- },
- messages : {
- "code" : {required : "請(qǐng)輸入務(wù)編碼!",
- number : "請(qǐng)輸入數(shù)字!"
- }
- },
- errorPlacement: function( lable, element ){
- element.ligerHideTip();
- element.parent().ligerTip({ content: lable.html(), target: element[0] });
- },
- success : function( lable ){
- lable.ligerHideTip();
- lable.remove();
- },
- submitHandler: function(formTar){
- var serialStr = $("#appEdit_Form").serialize();
- var urlStr = './appIndex.htm?actionMethod=addAppInfo';
- $.ajax({
- type:'post',
- url:urlStr,
- data:serialStr,
- success:function(redata){
- if (redata ==1) {
- alert( '操作成功!');
- }else{
- alert('失敗提示', '操作失敗!');
- return;
- }
- }
- });
- }
- });
- });
-
- //-->
- </script>
- </head>
-
- <body >
- <div class="right-body" >
- <form method="post" action="javascript:void" id="appEdit_Form" >
- <table style="width:400px;" border="0" cellpadding="0" cellspacing="0"
- class="viewdatagrid">
- <tbody>
- <tr>
- <th width="160">編碼:</th>
- <td width="0" ><input id="code" name="code" type="text" style="width: 240px" value="" level="2"/>
- </td>
- </tr>
- </tbody>
- </table>
-
- <div class="btn-view-block">
- <div class="toolbar">
- <ul>
- <li>
- <a href="javascript:void 0;" class="save" id="appEdit_btok" name="appEdit_btok" ><span>保存</span></a>
- </li>
- <li>
- </ul>
- </div>
- </div>
- </form>
- </div>
- </body>
- </html>
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。