uniapp中用vue3实现表单校验

<template>
	<view class="box">
		<view class="login_card">
			<view class="title">智能制造管理系统</view>
			<uni-forms class="forms" :model="form" ref="formRole">
				<uni-forms-item name="username" :rules="[{ required: true, errorMessage: '请输入用户名' }]">
					<uni-easyinput prefixIcon="person-filled" type="text" v-model="form.username" placeholder="请输入用户名"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item name="password" :rules="[{ required: true, errorMessage: '请输入密码' }]">
					<uni-easyinput prefixIcon="locked-filled" type="password" v-model="form.password" placeholder="请输入密码"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item name="code" :rules="[{ required: true, errorMessage: '请输入验证码' }]">
					<uni-easyinput class="left" prefixIcon="checkbox"  v-model="form.code"  placeholder="请输入验证码" ></uni-easyinput>	
				</uni-forms-item>
				<button @click="submit" class="btn">登入</button>
			</uni-forms>
		</view>
	</view>
</template>
   
<script setup lang="ts">
import { reactive, ref, onMounted, computed, watch } from 'vue';
const formRole = ref<any>(null);
const form = reactive({
	username: null,
	password: null,
	code:null,
	uuid: null
});
const submit = async () => {
	formRole.value.validate().then(async ({ valid, errors }: any) => {
		if (valid) {
		} else {
            //验证成功
			login(form).then((res) => {
				console.log(res)
			});
		}
	});
};
</script>

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
以下是使用Vue 3和uni-app实现表单校验的示例代码: ```vue <template> <div> <form @submit.prevent="submitForm"> <label for="name">姓名:</label> <input type="text" id="name" v-model="formData.name" /> <p v-if="errors.name" class="error">{{ errors.name }}</p> <label for="email">邮箱:</label> <input type="email" id="email" v-model="formData.email" /> <p v-if="errors.email" class="error">{{ errors.email }}</p> <button type="submit">提交</button> </form> </div> </template> <script> import { ref } from 'vue'; export default { setup() { const formData = ref({ name: '', email: '', }); const errors = ref({}); const validateForm = () => { errors.value = {}; if (!formData.value.name) { errors.value.name = '请输入姓名'; } if (!formData.value.email) { errors.value.email = '请输入邮箱'; } else if (!isValidEmail(formData.value.email)) { errors.value.email = '请输入有效的邮箱地址'; } return Object.keys(errors.value).length === 0; }; const submitForm = () => { if (validateForm()) { // 表单校验通过,提交表单 console.log('表单校验通过,提交表单'); } }; const isValidEmail = (email) => { // 简单的邮箱格式校验 const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailRegex.test(email); }; return { formData, errors, submitForm, }; }, }; </script> <style> .error { color: red; } </style> ``` 这个示例代码演示了如何使用Vue 3和uni-app实现表单校验。在模板,我们使用`v-model`指令绑定表单输入的值,并使用`v-if`指令根据校验结果显示错误信息。在`setup`函数,我们定义了`formData`和`errors`的响应式变量,并编写了`validateForm`函数来进行表单校验。最后,在提交表单时,我们调用`validateForm`函数来判断表单是否通过校验,如果通过则提交表单

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值