- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2024 01:28 AM - edited 01-19-2024 01:48 AM
I am using below script to autopopulate a single text field when any whole number given as input. i have 'amount' field if we enter 12 as input it should be autopopulate as '$ 12.00' otherwise it have to show error.
ex: input : 12
to : $ 12.00
but this was not working... properly
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var num = /(^[0-9]+,[0-9]*$)/;
if (!num.test(newValue)) {
g_form.showFieldMsg('amount','enter the whole number','info');
g_form.clearValue('amount');
} else {
alert('its a number');
var va = '$ ' + parseFloat(newValue).toFixed(2);
alert(va);
g_form.setValue('amount', va);
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2024 04:10 AM - edited 01-19-2024 04:11 AM
Hi @Aditya02
g_form.setValue call is triggering the onChange script again, causing an infinite loop that eventually clears the value. To prevent this, you can add a condition to check if the new value is already formatted correctly before attempting to format it again:
Here's a revised version of the script:
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);
}
}
Use of parseInt instead of parseFloat: Since you want to ensure the input is a whole number, it's better to use parseInt to convert the string to an integer. parseFloat is used for floating-point numbers, which you're trying to avoid.
Base parameter in parseInt: It's a good practice to specify the base (radix) when using parseInt. In this case, 10 is used for decimal numbers.
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2024 01:52 AM
Hi @Aditya02
There are a few issues with the script you've provided.
1. The regular expression you're using to validate the input is not correct for matching whole numbers. It seems to be looking for numbers with a comma, which is not necessary for whole numbers and does not match the example you've given (12).
2. You're using `alert` which is not recommended in ServiceNow as it can disrupt the user experience. Instead, you can use `g_form.showFieldMsg` to display messages.
3. When setting the value back to the field, you should set the raw number, not the formatted string, to avoid issues with further input validation or processing.
Here's a revised version of the script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Check if the newValue is a whole number
var num = /^[0-9]+$/;
if (!num.test(newValue)) {
// If not a whole number, show a message and clear the field
g_form.showFieldMsg('amount', 'Please enter a whole number', 'error');
g_form.clearValue('amount');
} else {
// If it's a whole number, format it as currency
var formattedValue = '$ ' + parseInt(newValue, 10).toFixed(2);
// Display the formatted value to the user without setting it as the field value
g_form.showFieldMsg('amount', 'Formatted value: ' + formattedValue, 'info');
}
}
Please note the following changes:
- The regular expression now checks for whole numbers only.
- The `showFieldMsg` method is used to display messages instead of `alert`.
- The formatted value is shown to the user as an informational message, but the actual field value is not changed to the formatted string. This is to ensure that the field value remains a valid number for any further processing or validation.
If you want to display the formatted value within the field itself, you should ensure that the field is meant to hold string values and not used for numerical calculations later on. If that's the case, you can uncomment the `g_form.setValue` line to set the formatted string as the field value.
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2024 02:04 AM - edited 01-19-2024 02:07 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2024 04:10 AM - edited 01-19-2024 04:11 AM
Hi @Aditya02
g_form.setValue call is triggering the onChange script again, causing an infinite loop that eventually clears the value. To prevent this, you can add a condition to check if the new value is already formatted correctly before attempting to format it again:
Here's a revised version of the script:
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);
}
}
Use of parseInt instead of parseFloat: Since you want to ensure the input is a whole number, it's better to use parseInt to convert the string to an integer. parseFloat is used for floating-point numbers, which you're trying to avoid.
Base parameter in parseInt: It's a good practice to specify the base (radix) when using parseInt. In this case, 10 is used for decimal numbers.
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2024 08:30 PM
That's Great! Now all the things were getting correctly..