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);

    }

}

2 REPLIES 2

Rakesh_M
Kilo Sage

Hi @John Spencer ,
1.You can make a catalog item variable accept only number .
2.In the variable record ,Under Type Specification tab ,update the Validation Regex as per requirement.
var regex.png

Tanushree Maiti
Kilo Patron

Hi @John Spencer ,

 

Use OOB Validate Regex option and select Numeric there for each variable (Single Line Text).

 

TanushreeMaiti_2-1777108108367.png

 

 

Screenshot 2026-04-25 143132.png

 

 

Result: It will not accept any special character like $ or ',' or any alphabet. Only 0-9 numeric value is accepted.

TanushreeMaiti_0-1777107849964.png

 

TanushreeMaiti_1-1777107889146.png

 

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin: