使用jQuery的validation插件来完成表单的验证
2015-11-15 12:51
751 查看
**使用jQuery的validation插件来完成表单的验证**
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>validation 练习</title> <link rel="stylesheet" type="text/css" href="../reset.css"> <link rel="stylesheet" type="text/css" href="style.css"> <script type="text/javascript" src="../jquery-1.9.1.js"></script> <script type="text/javascript" src="jquery.validate.js"></script> <script type="text/javascript" src="jquery.validate.messages_cn.js"></script> <script type="text/javascript" src="index.js"></script> <style type="text/css"> #content{width:800px;margin:30px auto;} #content h1{text-align: center;line-height: 50px;font-size: 20px;} #customValidation p{height: 50px;overflow: hidden;} #customValidation p label{text-align: left;width:100px;float: left;line-height: 40px;font-size: 15px;color:#707070;} #customValidation p input{float: left;width: 300px;height: 18px;line-height: 18px;border: 1px solid #ccc;padding: 10px 0;margin-right: 5px;font-size: 15px;text-indent: 5px;} #customValidation p span{float: left;line-height: 40px;} #customValidation p label.error{float: right;width: 280px;text-align: left;} #customValidation p input.submit{height: 30px;width: 60px;line-height: 30px;padding: 0;text-align: center;color: #777;cursor: pointer;margin-left: 100px;} #customValidation p em{height: 30px;line-height: 30px;margin-left: 5px;} #customValidation p em.error{background: url(zhuce2.gif) 0 0 no-repeat;padding-left:18px;padding-bottom: 2px;} #customValidation p em.success{background: url(zhuce3.gif) 0 0 no-repeat;padding-left:18px;padding-bottom: 2px;} </style> </head> <body> <div id="content"> <h1>表单验证</h1> <form id="customValidation" action=""> <p> <label for="cuser">用户名:</label> <input type="text" id="cuser" name="username" > <span>*</span> </p> <p> <label for="cpass">密码:</label> <input type="password" id="cpass" name="password" > <span>*</span> </p> <p> <label for="cemail">电子邮件:</label> <input id="cemail" name="email" > <span>*</span> </p> <p> <label for="curl">网址:</label> <input id="curl" name="url" > </p> <p> <label for="cvalcode">验证码:</label> <input id="cvalcode" name="valcode" >=7+9 </p> <p> <input type="submit" class="submit" value="提交"> </p> </form> </div> <script type="text/javascript"> $(function(){ $("#customValidation").validate({ rules:{ username:{ required:true, minlength:2 }, password:{ required:true, minlength:6 }, email:{ required:true, email:true }, url:"url", valcode:{ formula:"7+9" } }, messages:{ username:{ required:"请输入用户名", minlength:"至少输入两个字符", }, password:{ required:"请输入密码", minlength:"至少输入六个字符", }, email:{ required:"请输入邮箱", email:"请输入正确的邮箱地址", }, url:{ url:"请输入正确的网址", } }, errorElement:"em", success:function(label){ label.text(" ").addClass("success"); } }); }) </script> <script type="text/javascript"> $(function(){ $.validator.addMethod( "formula", function(value,element,param){ return value == eval(param); }, "请正确输入数学公式计算后的结果" ) }) </script> </body> </html>
源码下载地址:http://pan.baidu.com/s/1eQfQPfs
相关文章推荐
- 多选标签 用到jQuery
- WEB前端开发都应知道的jquery小技巧及jquery三个简写
- 【译】前端开发者都应知道的 jQuery 小技巧
- JQueryUI
- Jquery Ajax请求方法小结(值得收藏)
- jQuery 发送 post 请求返回字符串乱码的解决
- jQuery滑动选取数值范围
- JQuery中的回调对象
- KindEditor提交用jquery获取不到数据的解决方法
- 如何创建对象以及jQuery中创建对象的方式
- JQuery+EasyUI轻松实现步骤条效果
- WEB前端开发都应知道的jquery小技巧及jquery三个简写
- 10个常用的JQUERY代码片段
- jquery实现密码和确认密码的输入框中不能复制,粘贴,剪切,和右键
- jquery平滑滚动页面
- 用jQuery如何实现删除子节点和winform label显示数据分页
- JQuery中$.ajax()方法参数详解
- 常用Jquery前端操作
- Jquery选择器
- jquery实现漂亮文件上传表单样式