- 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-12-2025 02:19 AM - edited 03-13-2025 02:18 AM
Hi @niveditakumari,
You can create regex for this variable to add '$' in the beginning and followed by number which will be ^\$\d+(\.\d{2})?$ and add it to Validation Regex in Type Specifications section of Variable record.
If you found my reply helpful, please mark it as Solution and Helpful.
Thanks and Regards,
Ehab
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2025 02:39 AM
Hi @Ehab Pilloor,
Any step that you can help with.
Can you please help me with step for that.
Regards,
Nivedita
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2025 02:17 AM - edited 03-13-2025 02:20 AM
Hi @niveditakumari,
1. You can navigate to All -> Variable Validation Regex.
2. Create a new record.
3. Fill in the mandatory details like adding Name, Validation Regex and Validation Message.
4. Save it.
5. Use the Validation Regex in the Type Specification Section of Variable record.
6. Save the record and test on the catalog view.
Note: This is the no code solution for this requirement and follows best practice.
Regards,
Ehab
- 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