uniapp 设置form表单以及验证密码

事例:

代码:

复制代码
<template>
	<view class="changePwd">
		<view class="login_form">
			<view class="title">
				<view>
					修改密码
				</view>
				<view>
					您好,请在下方输入原密码和新密码进行更改密码
				</view>
			</view>
			<uni-forms :modelValue="form" ref="loginForm" :rules="rules">
				<uni-forms-item name="oldPassword">
					<view class="itemContent">
						<text class="itemTextStyle">原密码:</text>
						<input class="itemInputStyle" type="text" v-model="form.oldPassword" placeholder="请输入内容" />
					</view>
					<!--分割线-->
					<view style="height: 1px; background-color: #ccc;"></view>
				</uni-forms-item>
				
				<uni-forms-item name="newPassword">
				<view class="itemContent">
					<text class="itemTextStyle">新密码:</text>
					<input class="itemInputStyle" type="text" v-model="form.newPassword" placeholder="请输入内容" />
				</view>
				<!--分割线-->
				<view style="height: 1px; background-color: #ccc;"></view>
				</uni-forms-item>
				
				<uni-forms-item name="confirmPassword">
				<view class="itemContent">
					<text class="itemTextStyle">确认密码:</text>
					<input class="itemInputStyle" type="text" v-model="form.confirmPassword" placeholder="请输入内容" />
				</view>
				<!--分割线-->
				<view style="height: 1px; background-color: #ccc;"></view>
				</uni-forms-item>
				
				<view class="updateBtn">
					<button class="button" @click="updateForm">修改密码</button>
				</view>
			</uni-forms>
		</view>
	</view>
</template>
<script>
import {updatePassword} from '@/api/api.js';

	export default {
		name:"changePassword",
		data(){
			return{
				form:{
					oldPassword:'',
					newPassword:'',
					confirmPassword:''
				},
				rules:{
					// 旧密码规则限制
					oldPassword: {
						rules: [
							{required: true,errorMessage: '请输入原密码', trigger: 'blur'},
							{minLength: 1,maxLength: 11, errorMessage: '密码长度为1~11位', trigger: 'change'}
						]
					},
					// 新密码规则限制
					newPassword: {
						rules: [
							{required: true,errorMessage: '请输入新密码', trigger: 'blur'},
							{minLength: 1,maxLength: 11, errorMessage: '密码长度为1~11位', trigger: 'change'}
						]
					},
					// 确认密码规则限制
					confirmPassword: {
						rules: [
							{required: true,errorMessage: '确认密码不能为空'},
							{
								validateFunction: (rule, value, data) => data.newPassword === value,
								errorMessage: '两次输入的密码不一致'
							}
						]
					},
				},
			}
		},
		methods:{
			updateForm(){
				// 验证
				this.$refs.loginForm.validate().then(res=>{
					//console.log("res",res);			
				}).catch(err=>{
					console.log('表单数据错误信息:', err);
					/* uni.showToast({
						title:'登录失败',
						icon:'error',
					}) */
				});
				
				this.jumpPage(this.form);
			},
			async jumpPage(data){// 跳转页面
				
				const res = await updatePassword(data);
				if(res.success){
					uni.navigateBack({
					  url: '/pages/my/my'
					});
				}else{
					uni.showToast({
						title: "修改失败",
						icon: "error"
					}) 
				}
			
				
				
			},
		}
	}
	
	
</script>
<style lang="scss" scoped>
	uni-page-body {
		height: 100%;
	}
	
	.custom-font {
	    font-size: 16px; /* 设置字体大小 */
	    font-weight: bold; /* 设置字体粗细 */
	    color: black; /* 设置字体颜色 */
		align-items: center;
	    /* 可以继续添加其他字体样式 */
	  }

	.changePwd {
		overflow: hidden;
	}
	
	.login_form {
		/* background-color: rgba(0, 0, 0, 0.4); */
		border-radius: 5rpx;
		padding: 10rpx;
		margin: 3vw 20rpx;
	}

	.title view:nth-child(1) {
		font-size: 60rpx;
		margin-bottom: 8rpx;
	}
	
	.title view:nth-child(2) {
		font-size: 20rpx;
		margin-bottom: 30rpx;
		color: #b1b1b1;
	}

	.itemContent{
		display: flex;
		background-color: #ffffff;
		padding: 2vw 2vw;
		padding-top: 10rpx;
		box-sizing: border-box;
		align-items: center;
		.itemTextStyle{
			width: 90rpx;
			font-size: 16rpx;
			text-align: center;
		}
		.itemInputStyle{
			width: 90%;
		}
	}
	
	.updateBtn {
		margin-top: 140rpx;
		margin-left: 50rpx;
		margin-right: 50rpx;
		
		button {
			background-color: #66CD00;
			color: white;
		}
		
	}

	

</style>
相关推荐
CappuccinoRose11 分钟前
JavaScript 学习文档(二)
前端·javascript·学习·数据类型·运算符·箭头函数·变量声明
这儿有一堆花17 分钟前
Vue 是什么:一套为「真实业务」而生的前端框架
前端·vue.js·前端框架
宠友信息21 分钟前
2025社交+IM及时通讯社区APP仿小红书小程序
java·spring boot·小程序·uni-app·web app
全栈前端老曹31 分钟前
【MongoDB】深入研究副本集与高可用性——Replica Set 架构、故障转移、读写分离
前端·javascript·数据库·mongodb·架构·nosql·副本集
“负拾捌”1 小时前
python + uniapp 结合腾讯云实现实时语音识别功能(WebSocket)
python·websocket·微信小程序·uni-app·大模型·腾讯云·语音识别
NCDS程序员1 小时前
v-model: /v-model/ :(v-bind)三者核心区别
前端·javascript·vue.js
夏幻灵1 小时前
CSS三大特性:层叠、继承与优先级解析
前端·css
小杨同学呀呀呀呀1 小时前
Ant Design Vue <a-timeline>时间轴组件失效解决方案
前端·javascript·vue.js·typescript·anti-design-vue
qq_532453531 小时前
使用 Three.js 构建沉浸式全景图AR
开发语言·javascript·ar
华玥作者10 小时前
[特殊字符] VitePress 对接 Algolia AI 问答(DocSearch + AI Search)完整实战(下)
前端·人工智能·ai