问答详情
源自:1-2 如何开始及举例

代码是看着老师的代码敲的,可是运行有错误

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>validate-demo</title>
<style type="text/css">
body {
font-size: 36px;
line-height: 1.6;
}
p {
margin: 10px 0;
}
label {
display: inline-block;
min-width: 140px;
}
label .error {
margin-left: 10px;
color: red;
}
input,button {
line-height: 35px;
border: 1px solid #999;
min-width: 180px;
}
input .error {
border: 1px solid red;
}
input[type=submit],button {
margin-top: 20px;
font-size: 36px;
padding: 10px 0;
}
</style>
</head>
<body>
<form id="demoForm">
<fieldset>
<legend>用户登录</legend>
<p>
<label for="username">用户名</label>
<input type="text" id="username" name="username" />
</p>

<p>
<label for="password">密码</label>
<input type="password" id="password" name="password" />
</p>

<p>
<input type="submit" value="登录" />
</p>
</fieldset>
</form>
<script src="jquery-3.1.0.min.js"></script>
<script src="jquery.validate.js"></script>
<script>
$(document).ready(function(){
rules:{
username:{
required:true,
minlength:2,
maxlength:10
},
password:{
required:true,
minlength:2,
maxlength:16
}
},
messages:{
username:{
required:"用户名不能为空",
minlength:"用户名最少为2位",
maxlength:"用户名最多为10位"
},
password:{
required:"密码不能为空",
minlength:"密码最少为2位",
maxlength:"密码最多为16位"
}
}
});
</script>
</body>
</html>


提问者:weibo_猪猪猪惠婷_0 2016-11-01 21:10

个回答

  • 琢骨Lee
    2016-11-01 21:33:25

    $(function(){
            $('#idform').validate({
                rules:{
                    username:{
                        required:true,
                        minlength:2,
                        maxlength:6
                    },
                    password:{
                        required:true,
                        minlength:2,
                        maxlength:6
                    }
                }
            })
        });

    要获取表单ID

  • 琢骨Lee
    2016-11-01 21:32:17

    $('#idform').validate最重要的东西,你都没引用插件