#### 小小卡拉米 之 计算加法
<view class="addition">
<view class="num">
<view class="des">数值1(只允許數字):</view>
<view class="input_num"> <input type="text" v-model="default_num1" @input="inputnum1"></view>
</view>
<view class="num">
<view class="des">数值2(只允許數字):</view>
<view class="input_num"> <input type="text" v-model="default_num2" @input="inputnum2"></view>
</view>
<view class="num">
<view class="des">相加的結果:</view>
<view class="input_num" > {{changenum}}</view>
</view>
</view>
<view class="addition">
<view class="num">
<view class="des">数值1(只允許數字):</view>
<view class="input_num"> <input type="text" v-model="default_num1" @input="inputnum1"></view>
</view>
<view class="num">
<view class="des">数值2(只允許數字):</view>
<view class="input_num"> <input type="text" v-model="default_num2" @input="inputnum2"></view>
</view>
<view class="num">
<view class="des">相加的結果:</view>
<view class="input_num" > {{changenum}}</view>
</view>
</view>
##### JS
<script>
export default {
data() {
return {
default_num1:100,
default_num2:200,
};
},
methods: {
inputnum1(e) {
const o = e.target;
const inputRule = /^(0+)|[^\d]+/g //修改inputRule 的值
this.$nextTick(function() {
this.default_num1 = o.value.replace(inputRule , '');
})
},
inputnum2(e) {
const o = e.target;
const inputRule = /^(0+)|[^\d]+/g //修改inputRule 的值
this.$nextTick(function() {
this.default_num2 = o.value.replace(inputRule , '');
})
},
},
computed: {
changenum(){
return Number(this.default_num1) + Number(this.default_num2)
}
}
}
</script>
export default {
data() {
return {
default_num1:100,
default_num2:200,
};
},
methods: {
inputnum1(e) {
const o = e.target;
const inputRule = /^(0+)|[^\d]+/g //修改inputRule 的值
this.$nextTick(function() {
this.default_num1 = o.value.replace(inputRule , '');
})
},
inputnum2(e) {
const o = e.target;
const inputRule = /^(0+)|[^\d]+/g //修改inputRule 的值
this.$nextTick(function() {
this.default_num2 = o.value.replace(inputRule , '');
})
},
},
computed: {
changenum(){
return Number(this.default_num1) + Number(this.default_num2)
}
}
}
</script>
input框常用的正则表达式
1.只能输入数字 const inputRule = /[^\d]/g 2.只能输入字母 const inputRule = /[^a-zA-Z]/g 3.只能输入数字和字母 const inputRule =/[\W]/g 4.只能输入小写字母 const inputRule =/[^a-z]/g 5.只能输入大写字母 const inputRule =/[^A-Z]/g 6.只能输入数字和字母和下划线 const inputRule =/[^\w_]/g //下划线也可以改成% 7.只能输入中文 const inputRule =/[^\u4E00-\u9FA5]/g 8.只能输入数字和小数点 const inputRule =/[^\d.]/g 验证手机号是否正确 var phone=15054054532; var strTemp = /^1[3|4|5|6|7|8|9][0-9]{9}$/; if(strTemp.test(phone)){ console.log("正确") }else{ uni.showToast({icon:'none',title:'请输入正确的电话'}); return false } 电话号码隐藏中间4位 var phonenumber=15054054532; var reg = /^(\d{3})\d{4}(\d{4})$/; this.phonenum=phonenumber.replace(reg, "$1****$2")
没有评论