Monday, 2 March 2020

Custom Currency field Validation

Use Below script on change of field value

This script will help you to restrict end user to provide currency value in "$0.00" format


function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    //Type appropriate comment here, and begin script below
    var cost_field = g_form.getValue('cost_field');
    cost_field = cost_field.trim();
    // first character should be dollar sign
    var firstChar = cost_field.substring(0, 1);
    if (firstChar != '$') {
        alert("Please enter cost_field in $0.00 format");
        g_form.setValue("cost_field", oldValue);
        return;
    }
    // characters after the $ sign should be numerics
    var cost_fieldType = isNaN(cost_field.substring(1));
    if (cost_fieldType == true) {
        alert("Please enter cost_field in $0.00 format");
        g_form.setValue("cost_field", oldValue);
        return;
    }
    // entered value should have a decimal point
    var num = cost_field.substring(1);
    if (num.indexOf('.') == -1) {
        alert("Please enter cost_field in $0.00 format");
        g_form.setValue("cost_field", oldValue);
        return;
    }
    // there must be 2 digits only after the decimal
    var decNum = num.substring(num.indexOf('.') + 1, num.length);
    if (decNum.length != 2) {
        alert("Please enter cost_field in $0.00 format");
        g_form.setValue("cost_field", oldValue);
        return;
    }
}

No comments:

Post a comment