1.I have a field called software date(Date type) in one table, that should not accept future dates.

Archana23
Tera Contributor

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.

2 ACCEPTED SOLUTIONS

Ashish Parab
Mega Sage

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

View solution in original post

Runjay Patel
Giga Sage

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

-------------------------------------------------------------------------

View solution in original post

3 REPLIES 3

Ashish Parab
Mega Sage

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

Runjay Patel
Giga Sage

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

-------------------------------------------------------------------------

Ankur Bawiskar
Tera Patron
Tera Patron

@Archana23 

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

AnkurBawiskar_0-1738331508740.png

 

 

function onCondition() {

	// valid
	alert('Start date cannot be in future');
	g_form.showErrorBox('start_date_time','Invalid');
	g_form.setMandatory('start_date_time', true);

}

AnkurBawiskar_1-1738331508752.png

 

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader