【uni-app】input框限制小数点/整数位数

1.适用于uni-app小程序,可简单改造成源生小程序版

2.限制整数位只能为0-9999之间的数字

3.最多两位小数

4.首位为小数点时,变成0.

5.只能输入一个小数点

<template>
	<input class="inputPrice" :maxlength="maxLength" type="digit" v-model="currentPrice" @input="checkPrice">
</template>
<script>
	export default {
		data() {
			return {
				maxLength: 5,
				currentPrice: ''
			}
		},
		methods: {
			checkPrice: function(e) {
				let that = this;
				let price = e.detail.value
				let maxLength = price.indexOf('.');
				if (price.indexOf(".") < 0 && price != "") {
					//'超过4位则大于1万元'
					if (price.length > 4) {
						price = price.substring(0, price.length - 1)
						uni.showToast({
							title: '金额最高不能超过1万元',
							icon: 'none'
						})
					} else {
						price = parseFloat(price);
					}
				} else if (price.indexOf(".") == 0) {
					//'首位小数点情况'
					price = price.replace(/[^$#$]/g, "0.");
					price = price.replace(/\.{2,}/g, ".");
				} else if (!(/^(\d?)+(\.\d{0,2})?$/.test(price))) {
					//去掉最后一位
					price = price.substring(0, price.length - 1)
				}
				that.$nextTick(function() {
					//'有小数点时,最大长度为7位,没有则是5位'
					that.maxLength = maxLength == -1 ? 5 : 7
					that.currentPrice = price
				})

			}
		}
	}
</script>

 

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是一个使用 Uni-app 和 @paddlejs-models/facedetect 进行人脸检测的完整代码示例: 1. 在 `pages/index/index.vue` 中添加以下代码: ```vue <template> <view class="container"> <image src="{{ imagePath }}" mode="aspectFit" @click="detectFaces"></image> <view v-for="(face, index) in faces" :key="index" class="face-box" :style="{ top: face.top, left: face.left, width: face.width, height: face.height }"></view> </view> </template> <script> import { load } from '@paddlejs-models/facedetect'; export default { data() { return { imagePath: '/static/image.jpg', // 替换为你的图像路径 faces: [], }; }, async mounted() { await load(); }, methods: { async detectFaces() { const image = document.getElementById('image'); const result = await faceDetect(image); this.faces = result.map((face) => ({ top: face.top, left: face.left, width: face.right - face.left, height: face.bottom - face.top, })); }, }, }; </script> <style> .container { position: relative; } .face-box { position: absolute; border: 2px solid red; } </style> ``` 2. 将你的图像文件命名为 `image.jpg` 并放置在 `static` 目录下。 3. 运行 Uni-app 项目,你将看到一个显示图像并在图像中检测出人脸的界面。点击图像即可触发人脸检测操作,检测结果将在界面上以红色方的形式展示出来。 请确保已正确安装 @paddlejs-models/facedetect,并在项目中引入该库。另外,根据你的实际需求,可能需要适当调整代码中的图像路径和样式。 这是一个简单的示例代码,你可以根据具体需求进行修改和优化。希望能帮助到你!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值