- 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 03:04 AM
Hello @niveditakumari
Follow these steps:
- Navigate to All -> Variable Validation Regex
- Create a New record and add Regular expression ^\$\d+(\.\d{2})?$
- Add this Regex to the variable in type specification section
Note:
- This ensures the field only accepts values starting with $
- Now, users must enter values like $1000.00 or $1000 and incorrect formats (e.g., 1000.00 or 1000) will not be accepted.
Hope this helps!
"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"
Thank You
Juhi Poddar
- 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 04:46 AM
Thank you for marking my response as helpful.
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-17-2025 05:57 AM
Hi @Ankur Bawiskar,
It is not taking any value. When I'm adding any value it is giving error.
Please see attached screenshot :
Can you please correct that.
Regards,
Nivedita
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2025 06:28 AM
you need to tweak it as per your requirement.
Few of the regex you can refer
Basic format (matches amounts like $100, $100.50):
\$\d+(\.\d{2})?
Including commas for thousands (matches amounts like $1,000, $1,000.50):
\$\d{1,3}(,\d{3})*(\.\d{2})?
Optional cents (matches amounts like $100, $100.50, $100.5):
\$\d+(\.\d{1,2})?
Allowing spaces (matches amounts like $ 100, $ 1,000.50):
\$\s?\d{1,3}(,\d{3})*(\.\d{2})?
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