jQuery学习:jQuery插件--jQuery Validate

 jQuery库

jQuery Plugin Registry

使用菜鸟提供的cdn导入

设置出错的样式

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/lib/jquery.js"></script>
		<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/jquery.validate.min.js"></script>
	<style>
		.error{
			color: red;
		}
	</style>
	</head>
	<body>
		<!-- action是目标地址 -->
		<form action="#" id="form">
			
			<p>用户名(必须,最小6位):<input type="text" name="username" required minlength="6"></p>
			<p>密码(必须6-8位):<input id="#pwd1" type="password" name="pwd1" required  minlength="6" maxlength="8"></p>
			<p>确认密码(与密码相同):<input type="password" name="pwd2" required equalTo="#pwd1"></p>
			<p><input type="submit" value="提交"></p>
			
		</form>
		<script>
			$('#form').validate();//开启验证
		</script>
	</body>
</html>

重新定义表单规则:

声明式验证:只需要i声明各种验证规则,可以自定义验证失败信息

			$('#form').validate({
				messages: {
					// 属性名是输入框内部的值
					username: {
						required: '用户名是必须的',
						minlength: '用户名至少6位'
					},
					pwd1: {
						required: '密码是必须的',
						minlength: '密码至少为6位',
						maxlength: '密码最多为8位'
					},
					pwd2: {
						required: '请输入密码',
						minlength: '密码至少为6位',
						maxlength: '密码最多为8位',
						equalTo: '两次密码输入不一致'
					},

				}
			}); //开启验证
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/lib/jquery.js"></script>
		<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/jquery.validate.min.js"></script>
		<style>
			.error {
				color: red;
			}
		</style>
	</head>
	<body>
		<!-- action是目标地址 -->
		<form action="d" id="form">

			<p>用户名(必须,最小6位):<input type="text" name="username" required minlength="6"></p>
			<p>密码(必须6-8位):<input id="s" type="text" name="pwd1" required minlength="6" maxlength="8"></p>
			<p>确认密码(与密码相同):<input type="text" name="pwd2" required minlength="6" maxlength="8" equalTo="#s"></p>
			<p><input type="submit" value="提交"></p>

		</form>
		<script>
			$('#form').validate({
				messages: {
					// 属性名是输入框内部的值
					username: {
						required: '用户名是必须的',
						minlength: '用户名至少6位'
					},
					pwd1: {
						required: '密码是必须的',
						minlength: '密码至少为6位',
						maxlength: '密码最多为8位'
					},
					pwd2: {
						required: '请输入密码',
						minlength: '密码至少为6位',
						maxlength: '密码最多为8位',
						equalTo: '两次密码输入不一致'
					},

				}
			}); //开启验证
		</script>
	</body>
</html>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值