ngFormBuilder
ngFormBuilder copied to clipboard
custom validation on currency component not working (and some more)
F.e. var amount = parseFloat(input.replace(','').replace('.',',')); valid = (amount >= 1000 && amount <= 100000) ? true : 'Amount should be between 1,000 en 100,000';
tested with http://codepen.io/travist/full/xVyMjo/
ngFormBuilder-full.js:75402 Uncaught TypeError: Cannot read property 'charAt' of undefined
at HTMLInputElement.
Furthermore an amount with a period as last or first char could give problems and a custom validition (the one above f.e.) could give a problem when editing an amount (comma insert problem, f.e. 11,1111 after typing 111111).
My solutions:
element.bind('keyup', function() {
// var data = scope.data[scope.component.key]; SHO change 03/31/2017:
// data appears undefined sometimes, replaced by following
var data = $('[name="' + scope.component.key + '"]')[0].value;
For last char = period:
// SHO change 03/27/2017 added to prevent error on numbers ending or starting with a period
element.bind('blur', blurHandler);
function blurHandler() {
var data = scope.data[scope.component.key]
var doApply = false
// if last character is a comma
if (data.length > 0) {
if (data.charAt(data.length - 1) == '.') {
if(data.length === 1) {
data = "0.00"
}
else {data = data + "00"}
doApply = true
}
else if (data.charAt(0) == '.') {
data = "0" + data
doApply = true
}
if (doApply === true) {
scope.$apply(function() {
scope.data[scope.component.key] = data;
})
}
}