vue项目中限制input只能输入一个小数点及后两位number
2019-03-06 本文已影响130人
程序猿阿峰
我这里用的是vue
移动端的vant
框架中的NumberKeyboard 数字键盘组件
如下图这种效果 ↓
image.png
1.安装vue脚手架
推荐使用 Vue 官方提供的脚手架 Vue Cli 3 创建项目
npm install -g @vue/cli
2.创建一个项目
vue create hello-world
3.安装 vant
npm 安装
npm i vant -S
** yarn 安装**
yarn add vant
4.引入组件
使用 babel-plugin-import (推荐)
babel-plugin-import 是一款 babel 插件,它会在编译过程中将 import 的写法自动转换为按需引入的方式
安装 babel-plugin-import 插件
npm i babel-plugin-import -D
// .babelrc 中配置
// 注意:webpack 1 无需设置 libraryDirectory
{
"plugins": [
["import", {
"libraryName": "vant",
"libraryDirectory": "es",
"style": true
}]
]
}
5.引用组件,实现效果图
<template>
<!-- 抽奖奖金提现页面 -->
<div class="withDrawals-wrapper">
<!-- 选择银行卡 -->
<div class="bank-wrapper">
<div class="logo"></div>
<div class="content">
<div class="name">中国建设银行</div>
<div class="number">尾号8015储蓄卡</div>
</div>
</div>
<!-- 输入提现金额 -->
<div class="amount-wrapper">
<div class="amount-title">提现金额</div>
<div class="amount-count">
<span class="yuan">¥</span>
<van-field
class="count"
onfocus="this.blur()"
v-model="withDrawalsAmount"
placeholder="请输入提现金额"
/>
</div>
<div class="balance-wrapper">
<span
class="balance"
>可用余额{{balance}}元</span>
<span
class="all-btn"
>全部提现</span>
</div>
<van-button
type="primary"
:disabled="isHasInputValue"
size="large"
style="background-color: #128ee8; border-color: #128ee8;"
>预计两小时内到账,确认提现</van-button>
</div>
<!-- 输入键盘 -->
<van-number-keyboard
:show="isShowKeyBoard"
theme="custom"
extra-key="."
close-button-text="完成"
@blur="show = false"
@input="onClickInputKeyBoard"
@delete="onDeleteKeyBoard"
/>
</div>
</template>
// 按需引用组件
import { NumberKeyboard } from 'vant';
Vue.use(NumberKeyboard);
export default {
data() {
return {
isShowKeyBoard: true, // 是否显示虚拟键盘
withDrawalsAmount: '', // 提现金额
balance: '0.03' // 可用余额
}
},
methods: {
/**
* 模拟键盘点击数字时触发的函数
*/
onClickInputKeyBoard (value) {
this.withDrawalsAmount += value
// 限制只能输入一个小数点及两位小数
this.withDrawalsAmount = this.withDrawalsAmount.toString().match(/^\d*(\.?\d{0,2})/g)[0] || null
},
/**
* 模拟键盘删除时触发的函数
*/
onDeleteKeyBoard () {
let flag = true
if (flag) {
// 删除掉字符串最后一个
this.withDrawalsAmount = this.withDrawalsAmount.substring(0, this.withDrawalsAmount.length - 1)
if (this.withDrawalsAmount.length === 0) {
flag = false
return false
}
}
}
}
}
// 用的是scss
.withDrawals-wrapper{
height: 100%;
font-size: 0.16rem;
// 选择银行卡
.bank-wrapper{
display: flex;
padding: 0.1rem;
margin-bottom: 0.1rem;
background-color: #fff;
box-shadow: 0px 0px 1px 0px rgba(51, 51, 51, 0.14);
.logo{
height: 0.4rem;
width: 0.4rem;
flex-shrink: 0;
background-color: #999;
}
.content{
flex: 1;
display: flex;
flex-direction: column;
justify-content: space-between;
padding-left: 0.1rem;
position: relative;
&::after{
content: '';
display: block;
width: 0.06rem;
height: 0.06rem;
border-top: 1px solid rgb(195, 195, 195);
border-right: 1px solid rgb(195, 195, 195);
position: absolute;
top: 50%;
margin-top: -0.03rem;
right: 0.1rem;
transform: rotate(45deg);
}
.name{
font-size: 0.16rem;
}
.number{
font-size: 0.14rem;
color: #808080;
}
}
}
// 输入提现金额
.amount-wrapper{
padding: 0.14rem 0.1rem;
background-color: #fff;
.amount-title{
color: #333;
}
.amount-count{
display: flex;
align-items: center;
padding: 0.2rem 0;
.yuan{
width: 0.5rem;
text-align: center;
font-size: 0.3rem;
}
// .count{}
}
.balance-wrapper{
display: flex;
justify-content: space-between;
padding: 0.1rem 0;
margin-bottom: 0.3rem;
font-size: 0.14rem;
.balance{
color: #808080;
}
.active{
color: #f80000;
}
.all-btn{
color: rgb(52, 152, 214);
}
}
}
}
记载下,方便自己以后重新回顾。