uniapp实现表单验证

2 篇文章 0 订阅
2 篇文章 0 订阅

平时我们经常会使用到表单,并且还需要实现密码的眼睛点击变化时,密码的内容也会在享受正常格式和密码格式之间来进行切换,在提交时会经常使用到表单验证,所以就自己简单的写了一个表单验证。

效果图:

 代码:

<template>
	<view class="updata">
		<!-- 个人信息 -->
		<view class="archives">
			<view class="archives-title">
				<text>基本信息</text>
			</view>
			<view class="archives-form">
				<view class="archives-form-item">
					<view class="afi-label">
						<text></text>
						<text>姓名</text>
					</view>
					<input type="text" v-model="fromdata.name" placeholder="请输入">
					<text></text>
				</view>
				<view class="archives-form-item" @click="bindPicker('gender')">
					<view class="afi-label">
						<text></text>
						<text>性别</text>
					</view>
					<input type="text" v-model="fromdata.gender" placeholder="请选择" disabled>
					<u-icon name="arrow-right" size="20" color="#aeacaf"></u-icon>
				</view>
				<view class="archives-form-item">
					<view class="afi-label">
						<text></text>
						<text>手机号</text>
					</view>
					<input type="text" v-model="fromdata.phone" placeholder="请输入">
					<text></text>
				</view>
				<view class="archives-form-item" @click="toUrl('/pagesAdmin/staffManagement/organization')">
					<view class="afi-label">
						<text></text>
						<text>组织结构</text>
					</view>
					<input type="text" v-model="fromdata.organization" placeholder="请选择" disabled>
					<u-icon name="arrow-right" size="20" color="#aeacaf"></u-icon>
				</view>
				<view class="archives-form-item">
					<view class="afi-label">
						<text></text>
						<text>所属门店</text>
					</view>
					<view style="color: gainsboro;">{{fromdata.organization}}</view>
				</view>
				<view class="archives-form-item">
					<view class="afi-label">
						<text></text>
						<text>密码</text>
					</view>
					<input v-show="oldPwdShow" type="password" v-model="fromdata.pwd" placeholder="至少8位数,包括字母和数字">
					<input v-show="!oldPwdShow" type="text" v-model="fromdata.pwd" placeholder="至少8位数,包括字母和数字">
					<view @click="changeShowOldPwd">
					<view v-show="oldPwdShow">
					<u-icon name="/static/icon/hidden.png" size="24" color="#aeacaf"></u-icon>
					</view>
					<view v-show="!oldPwdShow">
					<u-icon name="/static/icon/show.png" size="24" color="#aeacaf"></u-icon>
					</view>
					</view>
				</view>
				<view class="archives-form-item">
					<view class="afi-label">
						<text></text>
						<text>确认密码</text>
					</view>
					<input v-show="newPwdShow"  type="password" v-model="fromdata.pwd_confirm" placeholder="请再次输入密码">
					<input v-show="!newPwdShow"  type="text" v-model="fromdata.pwd_confirm" placeholder="请再次输入密码">
					<view @click="changeShowNewPwd">
					<view v-show="newPwdShow">
					<u-icon name="/static/icon/hidden.png" size="24" color="#aeacaf"></u-icon>
					</view>
					<view v-show="!newPwdShow">
					<u-icon name="/static/icon/show.png" size="24" color="#aeacaf"></u-icon>
					</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 选择权限包 -->
		<view class="archives">
			<view class="archives-title">
				<text>选择权限包</text>
			</view>
			<view class="archives-form">
				<view class="archives-form-item" @click="toUrl('/pagesAdmin/staffManagement/jurisdictionList')">
					<view class="afi-label">
						<text></text>
						<text>权限包</text>
					</view>
					<input type="text" v-model="fromdata.jurisdiction" placeholder="请选择" disabled>
					<u-icon name="arrow-right" size="20" color="#aeacaf"></u-icon>
				</view>
			</view>
		</view>
		<view class="bottom-btn">
			<button type="default" @click="submit(fromdata)">保存</button>
		</view>
		<!-- 选择器 -->
		<u-picker mode="multiSelector" :closeOnClickOverlay="true" :show="pickerShow" :columns="columns"
			@confirm="pickerConfirm" @cancel="pickerShow = false" @close="pickerShow = false"></u-picker>
	</view>
</template>

<script>
	import {stuffCreateApi} from '@/api/mine.js'
	export default {
		components: {},
		data() {
			return {
				fromdata:{
					name:'', //姓名
					gender: '男', //性别
					phone:'', //手机号
					organization:'', //组织结构
					shop_id:'', //所属门店
					pwd:'',//密码
					pwd_confirm:'',  //确认密码
					jurisdiction:[],  //权限包
				},
				columns: [], //选择器绑定数据
				gender: [ //性别
					['男', '女']
				],
				pickerShow: false, //picker选择器显示
				oldPwdShow:true,
				newPwdShow:true
			}
		},
		onLoad(option) {
			this.fromdata.organization = option.organization
		},
		onShow() {},
		methods: {
			toUrl(url){
				uni.navigateTo({
					url:url
				})
			},
			// 显示picker选择器 绑定选择器数据
			bindPicker(val) {
				this.columns = this[val];
				this.pickerShow = true;
			},
			// picker选择器  确定
			pickerConfirm(e) {
				console.log(e)
				this.fromdata.gender = e.value[0];
				console.log(this.form)
				this.pickerShow = false;
			},
			changeShowOldPwd(){		
				this.oldPwdShow = !this.oldPwdShow
			},
			changeShowNewPwd(){
				this.newPwdShow = !this.newPwdShow
			},
			submit(data){
				let phoneReg = /^[1][3,4,5,7,8,9][0-9]{9}$/
				let passwordReg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/
				if(!this.fromdata.name){
					uni.showToast({
						title:"名字不能为空",
						icon:"none"
					})
return
				}
if(!phoneReg.test(this.fromdata.phone)|| !this.fromdata.phone){
					uni.showToast({
						title:"手机格式不正确",
						icon:"none"
					})
return
				}
if(!this.fromdata.organization){
					uni.showToast({
						title:"请选择组织结构",
						icon:"none"
					})
return
				}
if(!passwordReg.test(this.fromdata.pwd)){
					uni.showToast({
						title:"密码长度应为8-16位,包含数字和字母",
						icon:"none"
					})
return
				}
if(this.fromdata.pwd_confirm !== this.fromdata.pwd){
					uni.showToast({
						title:"两次输入密码不一致,请重新输入",
						icon:"none"
					})
return
				}
if(!this.fromdata.jurisdiction){
					uni.showToast({
						title:"请至少选择一个权限包",
						icon:"none"
					})
return
				}

					stuffCreateApi(this.fromdata).then(res=>{
						console.log(res);
					})
			}
		}
	}
</script>

<style>
	button::after {
		border: none;
	}
</style>

<style scoped lang="scss">
	// 个人信息
	.archives {}

	.archives-title {
		padding: 20rpx 30rpx;
		background: #f5f2f8;
		font-size: 28rpx;
		color: #979698;
	}

	.archives-form {
		padding: 0 30rpx;
	}

	.archives-form-item {
		display: flex;
		align-items: center;
		padding: 30rpx 0;
		border-bottom: 1rpx solid #f5f2f8;

		input {
			width: 100%;
		}

		text {
			flex-shrink: 0;
		}
	}

	.afi-label {
		display: flex;
		align-items: center;
	}

	.afi-label>text:nth-child(1) {
		color: red;
		font-size: 26rpx;
		line-height: 30rpx;
		min-width: 20rpx;
	}

	.afi-label>text:nth-child(2) {
		min-width: 200rpx;
	}
	.bottom-btn {
		display: flex;
		justify-content: center;
		align-items: center;
		padding-bottom: 30rpx ;
		padding-top: 10rpx;
		box-shadow: -2rpx 0 20rpx 2rpx #e0e5eb;
		position: fixed;
		background: #fff;
		z-index: 10;
		left: 0;
		bottom: 0;
		width: 100%;
	}
	
	.bottom-btn>button {
		padding: 6rpx 0;
		width: 80%;
		border-radius: 50rpx;
		background: orange;
		color: white;
		font-size: 34rpx;
		border: 1rpx solid #f3f3f8;
		line-height: 80rpx;
	}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值