Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Can Yokohama Employee Center Share Catalog Script Among Different Scripts

John Spencer
Tera Contributor

Hello All,

 

I have 10 fields on a catalog item -- require numeric input.  I have not found a way to have the function that does the input validation appear just in one place and called from there.  I include the code I am using below.  

The onChange and then the function code itself.   Right now that code is repeated in the in each 'onChange' field.   The 'input validation' code works very well.  I deliberately disallow '$' and ',' (commas).

 

Any advice / recommendation appreciated.

 

This is the onChange function:

function onChange(control, oldValue, newValue, isLoading) {

    validateDollarAmount('recurring_cost_year_02', oldValue, newValue, isLoading);

}

 

 

Below is the code that is repeated:

 

function validateDollarAmount(fieldName, oldValue, newValue, isLoading) {

    if (isLoading) {

        return;

    }

 

    var raw = (newValue || '').trim();

 

    if (raw === '') {

        g_form.setValue(fieldName, '0.00');

        return;

    }

 

    // Only digits and one optional decimal with max 2 places

    var moneyPattern = /^\d+(\.\d{1,2})?$/;

 

    if (!moneyPattern.test(raw)) {

        alert('The input: ' + raw + ' is not a valid number. Enter a valid amount like 0, 12, or 12.34. Do not use $ or commas.');

 

        g_form.setValue(fieldName, oldValue || '0.00');

        return;

    }

 

    var amount = Number(raw);

 

    if (!Number.isFinite(amount) || amount < 0) {

        alert('Negative numbers are not allowed.');

        g_form.setValue(fieldName, oldValue || '0.00');

        return;

    }

 

    var normalized = amount.toFixed(2);

 

    if (newValue !== normalized) {

        g_form.setValue(fieldName, normalized);

    }

}

0 REPLIES 0