At the input, I want to accept decimal and floating number only. Max 5 digits are allowed before decimal point and max 2 digits are allowed after decimal point. Initially, I have defined my rules like this way:
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'
]
I want to add another rule which show error message if user type more than 2 digits after decimal point. After adding the last rule, it's not working.
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
]
How to make it is working?
CodePudding user response:
try this:
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'
]
this checks if split
result has more than one element first and then checks for the max digits after .