Format field to decimal

Valerie24
Tera Contributor

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!

3 REPLIES 3

DrewW
Mega Sage
Mega Sage

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.

 

SAI VENKATESH
Tera Sage
Tera Sage

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

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.