Autopopulate a field in a Catalog item

yaswanth
Tera Contributor

I have assigned a task i.e.,

 

I have a variable named 'price' in a catalog item. It was single line text, now i want get it populate when ever a user entered a value like this for example:

 

input:   23   ->  output:  $ 23.00

input:   2.3   ->  output:  $ 2.30

input:   'and'   ->  output:  has to give alert error

 

it has to accept only numbers, if a user enters other than numbers it has to give error or filed error message.

1 ACCEPTED SOLUTION

Aditya02
Tera Guru

Update a single- line text field in this format ‘ $ 23.00’ when user enters input as 23.

  • It will accepts only a whole number.

Sol:

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

    if (isLoading || newValue === '') {

        return;

    }

    // Regular expression to check for whole numbers

    var num = /^[0-9]+$/;

    // Regular expression to check for already formatted currency

    var currencyFormat = /^\$ [0-9]+\.[0-9]{2}$/;

    // Check if the new value is already formatted as currency

    if (currencyFormat.test(newValue)) {

        // The value is already formatted correctly, do nothing

        return;

    }

    // Check if the new value is a whole number

    if (!num.test(newValue)) {

        // Show error message if not a whole number

        g_form.showFieldMsg('amount', 'Please enter a whole number', 'error');

        g_form.clearValue('amount');

    } else {

        // Format the value as currency

        var formattedValue = '$ ' + parseInt(newValue, 10).toFixed(2);

        // Set the formatted value back to the field

        g_form.setValue('amount', formattedValue);

    }

}

View solution in original post

1 REPLY 1

Aditya02
Tera Guru

Update a single- line text field in this format ‘ $ 23.00’ when user enters input as 23.

  • It will accepts only a whole number.

Sol:

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

    if (isLoading || newValue === '') {

        return;

    }

    // Regular expression to check for whole numbers

    var num = /^[0-9]+$/;

    // Regular expression to check for already formatted currency

    var currencyFormat = /^\$ [0-9]+\.[0-9]{2}$/;

    // Check if the new value is already formatted as currency

    if (currencyFormat.test(newValue)) {

        // The value is already formatted correctly, do nothing

        return;

    }

    // Check if the new value is a whole number

    if (!num.test(newValue)) {

        // Show error message if not a whole number

        g_form.showFieldMsg('amount', 'Please enter a whole number', 'error');

        g_form.clearValue('amount');

    } else {

        // Format the value as currency

        var formattedValue = '$ ' + parseInt(newValue, 10).toFixed(2);

        // Set the formatted value back to the field

        g_form.setValue('amount', formattedValue);

    }

}