- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2024 11:50 PM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2024 12:14 AM
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);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2024 12:14 AM
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);
}
}