当前位置:网站首页>uniapp,微信小程序input正则校验只能输入为数字和小数点位数限制
uniapp,微信小程序input正则校验只能输入为数字和小数点位数限制
2022-07-21 06:40:00 【丿乐灬学】
微信小程序input正则校验只能输入为数字和小数点位数限制
工具类util.js
function checkNum(e) {
let val = e.target.value.replace(/(^\s*)|(\s*$)/g, "")
if (!val) {
return ''
}
var reg = /[^\d.]/g
// 只能是数字和小数点,不能是其他输入
val = val.replace(reg, "")
// // 保证第一位只能是数字,不能是点
val = val.replace(/^\./g, "");
// // 小数只能出现1位
val = val.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
// // 小数点后面保留2位
val = val.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');
console.log(val);
return val
}
module.exports = {
checkNum
}
页面引入util.js
<template>
<view class="form_wrap">
<view>
<view class="item_box">
<text class="item_tit">药剂(支)</text>
<input type="number" v-model="formParams.dosage" @input="checkNum($event)" class="input" placeholder="请输入药剂"/>
</view>
</view>
</view>
</template>
<script>
import Util from 'common/util.js';
export default {
components: {},
data() {
return {
formParams: {
dosage: '',
}
}
},
onShow() {
},
methods: {
checkNum (e) {
this.$nextTick(() => {
this.formParams.dosage = Util.checkNum(e)
})
},
}
}
</script>
<style>
.time_box{
background: #F4F4F4;
color: #888888;
height: 80rpx;
line-height: 80rpx;
margin: 0 -30rpx 30rpx -30rpx;
padding: 0 20rpx;
}
.submit_btn{
background: #3E78ED;
color: #fff;
border-radius: 20rpx;
height: 80rpx;
line-height: 80rpx;
/* width: 75%; */
text-align: center;
font-size: 32rpx;
margin: 160rpx auto 0;
}
.form_wrap{
padding:0 30rpx 30rpx 30rpx;
box-sizing: border-box;
}
.input {
padding-left: 30rpx;
color: #000;
height: 100%;
flex: 1;
}
.item_tit {
font-size: 32rpx;
margin-left: 36rpx;
color: #333;
}
.item_box {
display: flex;
align-items: center;
border-bottom: 1px solid #ddd;
height: 90rpx;
line-height: 90rpx;
margin-bottom: 30rpx;
}
</style>
边栏推荐
- [semidrive source code analysis] [module bringup] 37 - LCM driven bringup process
- docker搭建redis及集群
- [rm_ee_note] 1 gm6020 transceiver & simple PID debugging
- [终端_1]-Xshell 5 最火的终端软件!
- Uniapp introduces Tencent map
- Malloc and space Configurator
- Qt:qstring and qstringlist
- Es installation & IK Chinese parser
- Design and implementation of tcp/ip protocol stack LwIP: Part VII
- Software resources Encyclopedia
猜你喜欢
(pytorch进阶之路六)Swin Transformer实现
list解析<stl初级> (跑路人笔记)
[network security] common questions in interview -- SQL injection
malloc 和 空間配置器
He Bin's full text in C language, with notes for his own use, gives latecomers some ideas
Appium脚本启动参数如何设置
The classification of artificial neural network includes: the classification of artificial neural network includes
[daily training] 1260 2D mesh migration
牛客刷题 01——KiKi去重整数并排序(C语言)
Selenium被检测为爬虫,怎么屏蔽和绕过
随机推荐
怎么学自动化测试
TCP/IP协议栈Lwip的设计与实现:之六
Lecture 1 Overview
A collection of style problems of uniapp applets using the u-view framework
Carefree Manager installation
Solve the problem that the uploaded file of ftpclient is empty and 0 bytes are displayed
Difference between two lists
docker搭建redis及集群
token和session的区别,这个经典面试题值得一个更深入的回答
Leetcode sword finger offer II 061 And the minimum K number pairs***
[completion course] development process of IOT / embedded / MCU graduation design project
Es installation & IK Chinese parser
Redis official visualization tool, with high appearance value and powerful functions!
Appium获取和点击坐标,元素不方便定位时非常好用
&lt; a&gt; The tag jumps to the server page and realizes the transfer of parameters
多智能体系统集群协同控制实验平台详解与典型案例
Leetcode skimming: related topics of binary tree sequence traversal
TCP/IP协议栈Lwip的设计与实现:之七
Simple understanding of gateway
List parsing < STL elementary > (runner's notes)