首先在vue的main.js中注冊全局函數(shù),這里配合使用了element-ui的Message提示,所以得先引入
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);
/*金額格式化函數(shù)*/
Vue.prototype.moneyFormat = function (val){ //moneyFormat是函數(shù)名
let reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/
if(val){
if(reg.test(val)){
return val
}else {
ElementUI.Message.error('請輸入正確金額')
return '' "
}
}
}
/*然后在組件中input中調(diào)用此函數(shù)*/
<el-input v-model="test" @blur="test=moneyFormat(test)" placeholder="請輸入金額" ></el-input>