- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2025 02:12 AM
Hi,
I have created field in catalog item that is single line text and it should only take value in USD.
Can anyone please help me to achieve that.
Regards,
Nivedita
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2025 02:42 AM
Hi @niveditakumari
Can you try using this on-change client script on the field
function onChange(control, oldValue, newValue, isLoading) {
// If the form is loading, don't validate
if (isLoading) {
return;
}
// The value of the field being validated
var value = newValue;
// Regular Expression to match the US currency format: $13,000.00 or 13,000.00
var regex = /^\$?\d{1,3}(,\d{3})*(\.\d{2})?$/;
// Test if the value matches the regex
if (!regex.test(value)) {
// Show an error message if the value does not match the pattern
g_form.showFieldMsg('your_field_name_here', "Please enter a valid amount in USD format (e.g., $13,000.00).", "error");
g_form.setValue('your_field_name_here', ''); }
}
- Thank you ,
Sohail Ahmed Syed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2025 02:51 AM
Hi You can follow below example:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Regular expression for USD format (e.g., $100.00, $1,000,000.99, $0.50)
var usdRegex = /^\$\d{1,3}(,\d{3})*(\.\d{2})?$/;
if (!usdRegex.test(newValue)) {
alert("Please enter a valid USD amount in the format: $1,234.56");
g_form.clearValue('your_field_name'); // Clears incorrect input
}
}
Please marl correct/helpful if this helps you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2025 03:05 AM
try this without scripting using variable validation Regex and then attach to your variable
OR
use this onChange script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
g_form.hideFieldMsg('variableName');
// Regular expression to match USD values (e.g., 1234.56 or 1234)
var regExp = /^\$(\d{1, 3}(\, \d{3})*|(\d+))(\.\d{2})?$/;
if (!regExp.test(newValue)) {
g_form.showFieldMsg('variableName', "Please enter a valid USD amount (e.g., 1234 or 1234.56).", "error");
g_form.clearValue('variableName'); // variableName
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2025 03:54 AM
Hi @Sohail Ahmed ,
It is not showing field message.
Please find attached screenshot :
When I'm adding here wrong value then It is showing field in red color and then field is becoming empty but not showing field error message.
Can you please help me to correct that.
Regards,
Nivedita
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2025 05:33 AM
Hi @niveditakumari ,
Can you please try first clearing the field value and then showing the field message ?
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// The value of the field being validated
var value = newValue;
// Regular Expression to match the US currency format: $13,000.00 or 13,000.00
var regex = /^\$?\d{1,3}(,\d{3})*(\.\d{2})?$/;
// Test if the value matches the regex
if (!regex.test(value)) {
// Show an error message if the value does not match the pattern
g_form.setValue('field_name', '');
g_form.showFieldMsg('field_name', "Please enter a valid amount in USD format (e.g., $13,000.00).", "error");
}
}
Thanks,
Sohail Ahmed Syed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2025 05:59 AM
Hi @Sohail Ahmed ,
It is not taking any value. When I'm adding any value it is giving error.
Please see attached screenshot :
Can you please help me to correct that.
Regards,
Nivedita
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2025 07:09 AM
Hi @niveditakumari ,
Were you using validation regex or client script ? Client script should work fine
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2025 02:51 AM
Hi You can follow below example:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Regular expression for USD format (e.g., $100.00, $1,000,000.99, $0.50)
var usdRegex = /^\$\d{1,3}(,\d{3})*(\.\d{2})?$/;
if (!usdRegex.test(newValue)) {
alert("Please enter a valid USD amount in the format: $1,234.56");
g_form.clearValue('your_field_name'); // Clears incorrect input
}
}
Please marl correct/helpful if this helps you!