jq裏驗證插件的自定義方法Jquery.validator.addMethod()示例
阿新 • • 發佈:2017-11-13
placement ans rem 提示 .html orm turn comm char
- 最近寫驗證的時候感覺原生的驗證謝了一遍又一遍,就想到了“不要重復造輪子,學會管理自己的工具庫”這句名言,於是嘗試用jq的validator。 用過又發現需要自定義方法去驗證,於是去查官網,寫了Jquery.validator.addMethod()方法
- <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庫文件 -->
- <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();
- });
- //自定義評語內容驗證方法
- 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 : "請輸入務編碼!",
- number : "請輸入數字!"
- }
- },
- 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>
jq裏驗證插件的自定義方法Jquery.validator.addMethod()示例