Format field to decimal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2024 01:58 PM - edited 06-12-2024 02:03 PM
Hello,
I have a Single Line text variable (u_gpa) that I want to format so that the end user can only enter one decimal point. The format of the field should be as such X.X (i.e. 3.5). And the end user should not be able to submit anything below a 2.0 in this field. How would you recommend going about this?
Thank you in advance!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2024 02:03 PM
ServiceNow does not have an on key press so really all you can do is use an onChange client script to check the format of the data and reject it by setting it to a blank value. You could also change it to a decimal field and set the scale=1 and see if that gets you want you want.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2024 03:01 PM
Hi @Valerie24
You can use onchange client Script and below is the code :
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Regular expression to match the X.X format where X is a digit
var regex = /^\d\.\d$/;
// Check if the value matches the regex
if (!regex.test(newValue)) {
alert('GPA must be in the format X.X');
return;
}
alert(newValue);
if (newValue > 2.0) {
alert('GPA must not be less than 2.0');
}
}
Thanks and Regards
Sai Venkatesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-13-2024 05:10 AM
Thanks @SAI VENKATESH, unfortunately that script does not work for me. The alert appears anytime I enter any value into my GPA field regardless if it meets the format guidelines or not.