Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

how to decimal value in the field

chandan31
Tera Contributor

Hi All ,

 

I have created single line text that one hour field , in that only number is allowed that one (Indicate the number of hours (maximum 39 hours and one decimal place allowed   like 39.1) ) and character is not allowed , can you tell how to get this requirement.

Thanks,

Chandan

1 ACCEPTED SOLUTION

Prashant Ahire
Kilo Sage

Hello @chandan31 

use this onchange client script,

 

select your field here  - 

PrashantAhire_4-1702035312902.png

 

I have trial and tested this script and is working as expected.

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var fieldName = g_form.getValue('your_field_name');   //use your field name

    // Regular expression to match the desired format (maximum 39 hours and one decimal place)
    var regex = /^([1-9]|[1-3][0-9])(\.\d)?$/;

    // Check if the value matches the regex pattern
    if (!regex.test(fieldName)) {
        // If the value doesn't match, show an error message

        alert('Please enter a valid number with one decimal place, maximum 39 hours.');
        // Set the field value to an empty string or another default value

        g_form.setValue('your_field_name', '');
    }
    //Type appropriate comment here, and begin script below
}

 

Outputs:- 

1) Doesnt accept alphabets- 

PrashantAhire_0-1702035116151.png

2) will not accept more than 39

PrashantAhire_1-1702035157174.png

 

3)More then one decimal will not be accpeted

PrashantAhire_2-1702035189888.png

 

Please check and Mark Correct and Helpful if it really helps you.
Regards,
Prashant Ahire

 

 

 

 

 

View solution in original post

2 REPLIES 2

Prashant Ahire
Kilo Sage

Hello @chandan31 

use this onchange client script,

 

select your field here  - 

PrashantAhire_4-1702035312902.png

 

I have trial and tested this script and is working as expected.

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var fieldName = g_form.getValue('your_field_name');   //use your field name

    // Regular expression to match the desired format (maximum 39 hours and one decimal place)
    var regex = /^([1-9]|[1-3][0-9])(\.\d)?$/;

    // Check if the value matches the regex pattern
    if (!regex.test(fieldName)) {
        // If the value doesn't match, show an error message

        alert('Please enter a valid number with one decimal place, maximum 39 hours.');
        // Set the field value to an empty string or another default value

        g_form.setValue('your_field_name', '');
    }
    //Type appropriate comment here, and begin script below
}

 

Outputs:- 

1) Doesnt accept alphabets- 

PrashantAhire_0-1702035116151.png

2) will not accept more than 39

PrashantAhire_1-1702035157174.png

 

3)More then one decimal will not be accpeted

PrashantAhire_2-1702035189888.png

 

Please check and Mark Correct and Helpful if it really helps you.
Regards,
Prashant Ahire

 

 

 

 

 

Prashant Ahire
Kilo Sage

@chandan31 

Did this helped you?

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer. 
Thanks