- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2025 04:40 AM
1.I have a field called software date(Date type) in one table, that should not accept future dates.
2. One more field called percentage(integer)
- Maximum value that can be updated in Percentage field should be 100
Please help me to achieve this.
Thanks in advance.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2025 05:11 AM - edited 01-31-2025 05:18 AM
Hello @Archana23 ,
You can try the below onChange client script( when software date selects or changes ) for restricting selection of a future date.
Script -
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var softwareDate = newValue;
var currentDate = new Date();
var softDateObj = new Date(softwareDate);
// Validate the Software Date (no future date allowed)
if (softDateObj > currentDate) {
g_form.setValue('software_date', ''); // Optionally clear the field
g_form.showFieldMsg('software_date', 'Software Date cannot be in the future.');
} else {
g_form.hideFieldMsg('software_date'); // Hide the error message if the date is valid
}
}
For validating percentage field, please try below onChange script -
Script -
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var percentage = newValue;
// Regex pattern to check if the percentage is between 0 and 100 (including decimal precision)
var percentageRegex = /^([0-9]{1,2}(\.\d+)?|100(\.0+)?)$/;
// Validate the Percentage (between 0 and 100)
if (!percentageRegex.test(percentage)) {
g_form.setValue('u_percentage', ''); // Optionally clear the field if invalid
g_form.showFieldMsg('u_percentage', 'Percentage must be between 0 and 100.');
} else {
g_form.hideFieldMsg('u_percentage'); // Hide the error message if the percentage is valid
}
}
Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.
Thanks and Regards,
Ashish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2025 05:18 AM
Hi @Archana23 ,
For data validation you can use onchange client script.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
var currentDate = new Date();
var softwareDate = new Date(g_form.getValue('software_date')); // Assuming 'software_date' is the field name
if (softwareDate > currentDate) {
g_form.showFieldMsg('software_date', 'Future dates are not allowed.', 'error');
g_form.clearValue('software_date');
}
}
for percentage check you can use before BR.
(function executeRule(current, previous /*null when async*/) {
// Check if percentage field is greater than 100
if (current.percentage > 100) {
current.percentage = 100; // Reset to 100 if greater than 100
gs.addErrorMessage('Percentage cannot exceed 100%. It has been reset to 100%.');
}
})(current, previous);
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2025 05:11 AM - edited 01-31-2025 05:18 AM
Hello @Archana23 ,
You can try the below onChange client script( when software date selects or changes ) for restricting selection of a future date.
Script -
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var softwareDate = newValue;
var currentDate = new Date();
var softDateObj = new Date(softwareDate);
// Validate the Software Date (no future date allowed)
if (softDateObj > currentDate) {
g_form.setValue('software_date', ''); // Optionally clear the field
g_form.showFieldMsg('software_date', 'Software Date cannot be in the future.');
} else {
g_form.hideFieldMsg('software_date'); // Hide the error message if the date is valid
}
}
For validating percentage field, please try below onChange script -
Script -
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var percentage = newValue;
// Regex pattern to check if the percentage is between 0 and 100 (including decimal precision)
var percentageRegex = /^([0-9]{1,2}(\.\d+)?|100(\.0+)?)$/;
// Validate the Percentage (between 0 and 100)
if (!percentageRegex.test(percentage)) {
g_form.setValue('u_percentage', ''); // Optionally clear the field if invalid
g_form.showFieldMsg('u_percentage', 'Percentage must be between 0 and 100.');
} else {
g_form.hideFieldMsg('u_percentage'); // Hide the error message if the percentage is valid
}
}
Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.
Thanks and Regards,
Ashish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2025 05:18 AM
Hi @Archana23 ,
For data validation you can use onchange client script.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
var currentDate = new Date();
var softwareDate = new Date(g_form.getValue('software_date')); // Assuming 'software_date' is the field name
if (softwareDate > currentDate) {
g_form.showFieldMsg('software_date', 'Future dates are not allowed.', 'error');
g_form.clearValue('software_date');
}
}
for percentage check you can use before BR.
(function executeRule(current, previous /*null when async*/) {
// Check if percentage field is greater than 100
if (current.percentage > 100) {
current.percentage = 100; // Reset to 100 if greater than 100
gs.addErrorMessage('Percentage cannot exceed 100%. It has been reset to 100%.');
}
})(current, previous);
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2025 05:52 AM
For both use UI policy
I gave example for 1 field, you can add for percentage on similar lines
no complex script required
Use Catalog UI policy
Start Time [After] Today
function onCondition() {
// valid
alert('Start date cannot be in future');
g_form.showErrorBox('start_date_time','Invalid');
g_form.setMandatory('start_date_time', true);
}
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