uniapp中用vue3实现表单校验

该代码段展示了使用uni-app框架创建的一个登录页面,包括用户名、密码和验证码输入框,以及表单验证功能。当用户点击登录按钮时,会触发验证过程,如果表单有效,则调用login函数进行登录操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<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
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值