表单数据验证组件

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>Bootstrap表单样式一</title>
	<!-- 让页面样式根据设备大小进行调整显示 -->
	<meta name=viewport content="width=device-width,initial-scale=1">
	<script type="text/javascript" src="bootstrap/js/jQuery.js"></script>
	<script type="text/javascript" src="bootstrap/js/bootstrap.js"></script>
	<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.css">
	<!-- 导入验证文件 -->
	<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/jquery.validate.min.js"></script>
	<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.3/additional-methods.min.js"></script>
	<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/localization/messages_zh.js"></script>
	<script type="text/javascript" src="bootstrap/js/form_validate.js"></script>

</head>
<body>
	<div class="container">
		<form action="index_submit" class="form-horizontal" id="myform">
			<fieldset>
				<legend><span class="glyphicon glyphicon-user"></span>用户注册</legend>
				<div class="form-group" id="nameDiv">
					<label for="name" class="col-md-1 control-label">姓名</label>
					<div class="col-md-5">
						<input type="text" name="name" placeholder="请输入姓名" id="name" class="form-control">
					</div>
					<div class="col-md-3" id="nameSpan"></div>
				</div>
				<div class="form-group" id="pwdDiv">
					<label for="pwd" class="col-md-1 control-label">密码</label>
					<div class="col-md-5">
						<input type="password" name="pwd" placeholder="输入密码" id="pwd" class="form-control">
					</div>
					<div class="col-md-3" id="pwdSpan"></div>
				</div>
				<div class="form-group" id="ageDiv">
					<label for="age" class="col-md-1 control-label">年龄</label>
					<div class="col-md-5">
						<input type="text" name="age" placeholder="输入年龄" id="age" class="form-control">
					</div>
					<div class="col-md-3" id="ageSpan"></div>
				</div>
				<div class="form-group" id="emailDiv">
					<label for="email" class="col-md-1 control-label">邮箱</label>
					<div class="col-md-5">
						<input type="text" name="email" placeholder="输入邮箱" id="email" class="form-control">
					</div>
					<div class="col-md-3" id="emailSpan"></div>
				</div>
				<div class="col-md-2 col-md-offset-1">
					<input type="submit" class="btn btn-info" value="注册">
					<input type="reset"   class="btn btn-danger" value="重置">
				</div>
			</fieldset>
		</form>
	</div>
</body>
</html>
$(document).ready(function(){
	$("#myform").validate({
		debug:true,//取消表单的自动提交
		submitHandler:function(form){
			//实现表单提交的函数
			form.submit();
		},
		errorPlacement:function(error,element){
			//显示错误提示信息
			//将错误提示放在之前预留的div中
			$("#"+$(element).attr("id")+"Span").append(error);

		},
		highlight:function(element){
			$(element).fadeOut(1,function(){
				$(element).fadeIn(1);
				//将原来的蓝色改变为红色
				$("#"+$(element).attr("id")+"Div").attr("class","form-group has-error")
			})
		},
		unhighlight:function(element){
			$(element).fadeOut(1,function(){
				$(element).fadeIn(1);
				//将原来的蓝色改变为红色
				$("#"+$(element).attr("id")+"Div").attr("class","form-group has-success")
			})
		},
		errorClass:"text-danger",
		rules:{
			name:{
				required:true,
			},
			pwd:{
				required:true,
			},
			age:{
				required:true,
				digits:true
			},
			email:{
				required:true,
				email:true
			}
		}
	})
})

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue 3中,可以使用VeeValidate库进行表单数据校验。VeeValidate是一个非常强大的表单验证插件,可以用于Vue.js应用程序的表单验证。它提供了丰富的验证规则和自定义验证规则的能力。 首先,需要安装VeeValidate库。可以使用npm或yarn进行安装: ``` npm install vee-validate@next ``` 或者 ``` yarn add vee-validate@next ``` 安装完成后,在你的Vue组件中引入并使用VeeValidate。具体步骤如下: 1. 在你的Vue组件中,引入`defineRule`和`configure`函数: ```javascript import { defineRule, configure } from 'vee-validate'; ``` 2. 使用`defineRule`函数定义你需要的验证规则。例如,如果想要验证一个字段是否为必填字段,可以使用`required`规则: ```javascript defineRule('required', value => { if (!value || value.trim() === '') { return '该字段为必填字段'; } return true; }); ``` 3. 在Vue组件的`setup`函数中使用`configure`函数来配置VeeValidate的行为。可以指定要使用的验证规则、错误消息等: ```javascript configure({ validateOnInput: true, // 当输入时进行验证 validateOnChange: true, // 当值发生变化时进行验证 }); ``` 4. 在Vue模板中,使用`v-model`指令绑定表单字段,并使用`validation`属性获取验证结果。示例代码如下: ```html <template> <form @submit.prevent="submitForm"> <div> <label for="name">姓名</label> <input id="name" type="text" v-model="name" /> <span>{{ validation.name ? validation.name : '' }}</span> </div> <!-- 其他表单字段 --> <button type="submit">提交</button> </form> </template> ``` 5. 在Vue组件中,定义`submitForm`方法来处理表单提交事件: ```javascript import { useForm, useField } from 'vee-validate'; export default { setup() { const { handleSubmit } = useForm(); const { value: name, errorMessage: nameError } = useField('name'); const submitForm = handleSubmit(() => { // 表单提交逻辑 }); return { name, nameError, submitForm, }; }, }; ``` 上述步骤中,我们引入了VeeValidate库,并定义了一个`required`验证规则来验证字段是否为必填字段。然后,在Vue模板中使用`v-model`指令绑定字段,并使用`validation`属性获取验证结果。最后,在表单提交时调用`handleSubmit`方法来处理表单提交事件。 这样,你就可以在Vue 3中实现纯Vue表单数据校验了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值