在輸入時,我只想接受十進制和浮點數。小數點前最多允許 5 位數字,小數點后最多允許 2 位數字。最初,我以這種方式定義了我的規則:
priceRules: [
v => !!v || 'Price is required',
v => /^\d (\.\d )?$/.test(v) || 'Number only',
v => (v && v.toString().split('.')[0].length <= 5) || 'No more than 5 digits before the decimal point'
]
如果用戶在小數點后輸入超過 2 位數字,我想添加另一個顯示錯誤訊息的規則。添加最后一條規則后,它不起作用。
priceRules: [
v => !!v || 'Price is required',
v => /^\d (\.\d )?$/.test(v) || 'Number only',
v => (v && v.toString().split('.')[0].length <= 5) || 'No more than 5 digits before the decimal point',
v => (v && v.toString().split('.')[1].length > 2) || 'No more than 2 digits after the decimal point'. // this is not working
]
如何讓它作業?
演示
uj5u.com熱心網友回復:
嘗試這個:
priceRules: [
v => !!v || 'Price is required',
v => /^\d (\.\d )?$/.test(v) || 'Number only',
v => (v && v.toString().split('.')[0].length <= 5) || 'No more than 5 digits before the decimal point',
v => (v && v.toString().split('.').length < 2) || (v && v.toString().split('.')[1].length <= 2) || 'No more than 2 digits after the decimal point'
]
這首先檢查split結果是否有多個元素,然后檢查最大位數.
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/338424.html
標籤:Vue.js vuetify.js
