Hi,
I am trying to add custom validation for a field in my form (UPC) because wappler doesn’t have any built in validation for this. But I am unable to find a way to do it through wappler.
Does wappler have any way that’ll allow me to add custom client side validation that’ll run with other available validations?
Thank you
What would you like to validate? There’s a regex validation option, where you can enter any custom regexp pattern and validate the input.
But my validation is little large, can it be done by regexp ? it is to validate the UPC.
Following is my code:
function validateUPC(barcode) {
if (barcode.length < 8 || barcode.length > 18 ||
(barcode.length != 8 && barcode.length != 12 &&
barcode.length != 13 && barcode.length != 14 &&
barcode.length != 18)) {
return false;
}
var lastDigit = Number(barcode.substring(barcode.length - 1));
var checkSum = 0;
if (isNaN(lastDigit)) { return false; } // not a valid upc/ean
var arr = barcode.substring(0, barcode.length - 1).split("").reverse();
var oddTotal = 0, evenTotal = 0;
for (var i = 0; i < arr.length; i++) {
if (isNaN(arr[i])) { return false; } // can't be a valid upc/ean we're checking for
if (i % 2 == 0) { oddTotal += Number(arr[i]) * 3; }
else { evenTotal += Number(arr[i]); }
}
checkSum = (10 - ((evenTotal + oddTotal) % 10)) % 10;
// true if they are equal
return checkSum == lastDigit;
}