输入框限制只能输入数字

这段代码展示了如何在前端实现对用户输入的水箱容量进行验证,包括清除非数字字符、确保首位非小数点、限制最多一个小数点、最多16位长度等,确保输入符合规范。
proving(index) {
			// this.list[index].value 是输入框输入的值,这里是列表循环出来的输入框
			// 先把非数字的都替换掉,除了数字和.
			this.formDate.tankList[index].capacity = this.formDate.tankList[index].capacity.replace(
				/[^\d.]/g,
				'',
			)
			// 必须保证第一个为数字而不是.
			this.formDate.tankList[index].capacity = this.formDate.tankList[index].capacity.replace(
				/^\./g,
				'',
			)
			// 保证只有出现一个.而没有多个.
			this.formDate.tankList[index].capacity = this.formDate.tankList[index].capacity.replace(
				/\.{2,}/g,
				'',
			)

			// 保证.只出现一次,而不能出现两次以上
			this.formDate.tankList[index].capacity = this.formDate.tankList[index].capacity
				.replace('.', '$#$')
				.replace(/\./g, '')
				.replace('$#$', '.')
			let count = -1
			for (let i in this.formDate.tankList[index].capacity) {
				if (this.formDate.tankList[index].capacity[i] === '.') {
					count = i
				}
				if (count !== -1) {
					if (i - count > 2) {
						this.formDate.tankList[index].capacity = this.formDate.tankList[
							index
						].capacity.substring(0, this.formDate.tankList[index].capacity.length - 1)
					}
				}
			}
			// 限制输入长度最多为16位
			if (this.formDate.tankList[index].capacity.length > 16) {
				this.formDate.tankList[index].capacity = this.formDate.tankList[
					index
				].capacity.slice(0, 16)
			}
		},
	<Input
		v-model="e.capacity"
		placeholder="请输入水箱容量"
		@on-keyup="proving(idex)"
		></Input>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值