使用jQuery的validation插件来完成表单的验证

            **使用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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值