jquery.validate.js使用说明——后台添加用户邮箱功能:非空、不能重复、格式正确

重点内容为:  jQuery验证控件jquery.validate.js使用说明+中文API【http://www.tuicool.com/articles/iABvI3】

简单教程可以参考【jQuery的validate插件使用整理(传智播客_王昭珽)】

(一)需求介绍

管理员后台添加用户时,邮箱需要满足需求有

(1)不能为空,因为要支持用户使用邮箱登陆

(2)不恩重复,因为要用户使用邮箱登陆所有重复会让使用户登陆报错

(3)格式正确

(二)效果

(三)编码

(1)jsp页面

说明:f:text是自定义标签

       validate是一个基于Jquery的表单验证插件,利用他的remote可以用来自定义远程验证 JQuery validate插件Remote用法大全的相关资料(http://www.jb51.net/article/84220.htm) 关键是格式要正确:如   class里面的   email:{email:true},

 1 <td class="in-lab" width="15%">
 2 <s:message code="user.email"/>:</td>
 3 
 4 <td class="in-ctt" width="35%" >
 5 
 6 <f:text 
 7 name="email" 
 8 value="${oprt=='edit' ? (bean.email) : ''}"
 9 class="{
10     email:{email:true},
11     required:true,
12     remote:{url:'check_email.do',
13                   type:'post',
14                   data:{original:'${oprt=='edit' ? (bean.email) : ''}'}},
15     messages:{remote:'${emailExist}'}}"
16 
17 style="width:180px;"/>
18 </td>

jsp页面对user.email.exist的提示信息用变量emailExist进行了保存

1 <c:set var="emailExist">
2     <s:message code="user.email.exist"/>
3 </c:set>    

user.email.exist信息保存在配置文件中

1 user.email.exist=\u7535\u5B50\u90AE\u7BB1\u5DF2\u5B58\u5728  //电子邮箱已存在

 

(2)controller层方法

 1     /**
 2      * 检查邮箱是否存在
 3      * 
 4      * @return
 5      * @throws IOException 
 6      */
 7     @RequestMapping("check_email.do")
 8     public void checkEmail(String email, String original, HttpServletResponse response) {
 9         if (StringUtils.isBlank(email)) {
10             Servlets.writeHtml(response, "false");
11             return;
12         }
13         if (StringUtils.equals(email, original)) {
14             Servlets.writeHtml(response, "true");
15             return;
16         }
17         // 检查数据库是否重名
18         boolean exist = service.emailExist(email);
19         if (!exist) {
20             Servlets.writeHtml(response, "true");
21         } else {
22             Servlets.writeHtml(response, "false");
23         }
24     }

 

posted @ 2016-12-13 14:43  涤新云  阅读(1026)  评论(0编辑  收藏  举报