I want the currency field on my catalog item form.

dhineshkumar
Tera Guru

Hi Experts.

How to make hte currency field on my catalog form using onchange client script??

Example: If I enter 3863(any value) should be convert as $3,863.00. How to write the script using onchange method.

1 ACCEPTED SOLUTION

Shripal
Tera Expert

Hi,

You will have to use 2 Single line text variables to achieve this on the catalog item. One variable will be visible to the users on the portal and the other one will be hidden. In my example I have used "Currency" (name - currency, type - Single line Text) and "Currency Hidden" (name - currency_hidden, type - Single line Text).

I wrote an onChange Catalog Client script on the "currency" field: 

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	
	// My Field Names (you can change as per your value)
	var field_name = 'currency';
	var field_name_hidden = 'currency_hidden';

	if (g_form.getValue(field_name_hidden)) {
		g_form.clearValue(field_name_hidden);
		return;
	}
	
	// Check if the entered value is a valid Floating point value or not
	var reg = /^[+]?\d+(\.\d+)?$/;
	var valid = reg.test(newValue);
	if (!valid) {
		g_form.clearValue(field_name);
		alert("Please enter valid floating point numbers.");
		return;
	}

	var new_value = newValue;
	new_value = parseFloat(new_value).toFixed(2);      // Get the floating point value and modify the the amount to only 2 decimal points
	var amount = new_value.split(".");                 // Split the integer and float part of the amount
	var int_var = amount[0];
	var float_var = amount[1];
	var string = "0";

    var reverse = int_var.split('').reverse().join('');
    while (reverse != '') {
        var cut = reverse.substring(0,3);
        reverse = reverse.substring(3);
        cut = cut.split('').reverse().join('');
        if (string == "0") string = cut;
		else string = cut + "," + string;
    }

	// Add "$" and the float part to the integer and make the final string
	string = "$" + string + "." + float_var;
	
	// Set this field value in the hidden field
	// This will be used when this onChange code will be called again
	// we will check if the hidden field has value or not. (Line 10 if this script)
	// If the hidden field has any value that means that this onChange code should not run anymore. (Line 11, 12 of this script)
	// If the hidden field is blank, then only we will run this code completely.
	g_form.setValue(field_name_hidden, string);
	
	// Set the string to the field. - This will eventually make this code run again because the variable value has been changed. So this onChange script wil run again
	// But since the hidden variable has also this value, this code will not run again.
	g_form.setValue(field_name, string);
}

 

This will finally look like this: (Keeping "Currency Hidden" variable NOT hidden for complete visibility) 

1. When the value is entered

2. The final response after the script runs:

find_real_file.png

View solution in original post

13 REPLIES 13

Hi Nandhini, Thank you for pointing out the defect in the code. I have made the required changes in the code to overcome this problem. You can find the updated code in my previous comment above. Please use this updated code and let me know if that works. Thanks in advance.

Hi Shripal,

Thankyou so much for response,

 

This updated script is not working. The code is not running from the "var reverse "and throwing out the errror. Please help me on this.

 

 

 

 

 

This new script is not working from "var reverse" and its throwing error like "It is not a function" .Please look into it .

Hi Nandhini, can you please share a screenshot of the error? As I do not get any errors while running the code.

Getting error like"There is a error in javascript on your browser console " in portal and then checked in console its showing 

nandhinimuthu_0-1690991110965.png