jeesite 使用笔记(二) 字段合法性校验

一、字段的非空校验

         1、要达到的效果如下图所示,当没有填充或者只使用空格填充,点击【保存】,会弹出红色的【必填信息 *

                 

         2、        

                                 答案:

                                

                                            required"/>

                                            *

                                

                     

                          其中 required决定了弹出字段为【必填信息 *】,在文件【src/main/webapp/static/jquery-validation/1.11.0/jquery.validate.js】中定义,如图:

                         

 

二、字段的去重校验:

         1、需要达到的效果如下图,同一【所属页面】下不能有相同的【关键字】,当用户在【关键字】一栏填入非法数据后,点击别处时会弹出下图红色的【关键字已存在 *】

                 

         2、执行步骤:

                  (1)在js中对整个表单【inputForm】做验证:

                                  

                          上图中的【oldKeyword】用于用户修改数据时,防止修改前和修改后一致的情况错误的报出【关键字已存在信息】,即为了修改前后的新旧信息做比对      

                      $(document).ready(function(){

                                

                                 $("#inputForm").validate({

                                            rules:{

                                                       keyword:{ remote:{   type:"POST",

                                                                                        url:"${ctx}/knowledge/knowledgeLocal/checkUnique",

                                                                                        data:{

                                                                                                   oldKeyword:function(){return $("#oldKeyword").val();},

                                                                                                   newKeyword:function(){return$("#keyword").val();},

                                                                                                   belongToPage:function(){return$("#belongToPage").val();},

                                                                                                   id:function(){return$("#id").val();}

                                                                                        }

                                                                             }

                                                       }

                                            },

                                            messages:{

                                                       keyword:{remote: "关键字已存在"}

                                            },

                      其中【url】的参数为controller中对应验证的方法,data中的四个参数必须和controller中checkUnique方法一致,即checkUnique方法为下图所示:

                     



                  然后,在【checkUnique】方法中做查询数据库以及新旧数据的比对,若库中已经存在该关键字,返回 字符串 “false”,若没有或者newKeyword==oldKeyword ,返回 字符串 “true”

        

        

 

你可能感兴趣的:(jeesite)