Files
web/src/validate.js

45 lines
1.1 KiB
JavaScript

export function isEmpty(v) {
return v === null || typeof v === 'string' && v.trim() === ''
}
export function validateRequired(v) {
if (isEmpty(v))
return 'Required'
return true
}
export function validateAmount(v) {
if (isEmpty(v))
return true
const floatRegex = /^-?\d*(?:[.,]\d*?)?$/
if (!floatRegex.test(v))
return 'Amount must be a number'
if (parseFloat(v) <= 0)
return 'Amount must be positive'
return true
}
export function validateMax(v) {
if (!isEmpty(minPrice.value) && !isEmpty(v) && parseFloat(v) < parseFloat(minPrice.value))
return 'Must be greater than the minimum price'
return true
}
export function validateMin(v) {
if (!isEmpty(maxPrice.value) && !isEmpty(v) && parseFloat(v) > parseFloat(maxPrice.value))
return 'Must be less than the maximum price'
return true
}
export function validateTranches(v) {
const i = parseInt(v)
if (parseFloat(v) !== i)
return 'Whole numbers only'
if (i < 1)
return 'Must have at least one tranche'
if (i > 255)
return 'Maximum 255 tranches'
return true
}